php通過GD庫實(shí)現(xiàn)驗(yàn)證碼功能
首先看看實(shí)現(xiàn)的效果:

具體實(shí)現(xiàn):
<?php/*PHP實(shí)現(xiàn)驗(yàn)證碼*/
session_start();//開啟會話
//創(chuàng)建畫布
$image=imagecreatetruecolor(100,38);
//背景顏色
$bgcolor=imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$bgcolor);
$captch_code='';//存儲驗(yàn)證碼
//隨機(jī)選取4個數(shù)字
for($i=0;$i<4;$i++){
$fontsize=10; //
$fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));//隨機(jī)顏色
$fontcontent=rand(0,9);
$captch_code.=$fontcontent;
$x=($i*100/4)+rand(5,10); //隨機(jī)坐標(biāo)
$y=rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
/*//字母和數(shù)字混合驗(yàn)證碼
for($i=0;$i<4;$i++) {
$fontsize = 10; //
$fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));//??????
$data = 'abcdefghijklmnopqrstuvwxyz1234567890'; //數(shù)據(jù)字典
$fontcontent = substr($data, rand(0, strlen($data)), 1);
$captch_code.=$fontcontent;
$x = ($i * 100 / 4) + rand(5, 10);
$y = rand(5, 10);
imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}*/
$_SESSION['code']=$captch_code;
//增加干擾點(diǎn)
for($i=0;$i<200;$i++){
$pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);//
}
//增加干擾線
for($i=0;$i<3;$i++){
$linecolor=imagecolorallocate($image,rand(80,280),rand(80,220),rand(80,220));
imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
}
//輸出格式
header('content-type:image.png');
imagepng($image);
//銷毀圖片
imagedestroy($image);
實(shí)現(xiàn)表單的連接和“換一個”功能的實(shí)現(xiàn):1
<input type="text" placeholder="驗(yàn)證碼" name="verifycode" class="captcha"><br><br>
<img id="captcha_img" src="captcha.php?r=<?php echo rand();?>" alt="驗(yàn)證碼">
<label><a href="javascript:void(0)" rel="external nofollow" onclick="document.getElementById('captcha_img').src='captcha.php?r='+Math.random()">換一個</a> </label>
captcha.php后面加上一個隨機(jī)參數(shù)是為了實(shí)現(xiàn)換一個驗(yàn)證碼的功能,,換一個功能通過簡單的js即可實(shí)現(xiàn)。如果沒有這個功能可以不用參數(shù)。
總結(jié)
以上所述是小編給大家介紹的php通過GD庫實(shí)現(xiàn)驗(yàn)證碼功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Thinkphp連表查詢及數(shù)據(jù)導(dǎo)出方法示例
這篇文章主要介紹了Thinkphp連表查詢及數(shù)據(jù)導(dǎo)出方法,結(jié)合實(shí)例形式分析了thinkPHP連表查詢的操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-10-10
ThinkPHP框架整合微信支付之Native 掃碼支付模式二圖文詳解
這篇文章主要介紹了ThinkPHP框架整合微信支付之Native 掃碼支付模式,結(jié)合圖文形式詳細(xì)分析了thinkPHP整合微信掃碼支付功能的具體步驟與相關(guān)操作技巧,以及與模式一的區(qū)別,需要的朋友可以參考下2019-04-04
PHP輸出當(dāng)前進(jìn)程所有變量/常量/模塊/函數(shù)/類的示例
PHP輸出當(dāng)前進(jìn)程所有變量、常量、模塊、函數(shù)、類的示例2013-11-11
ThinkPHP里用U方法調(diào)用js文件實(shí)例
這篇文章主要介紹了ThinkPHP里用U方法調(diào)用js文件的方法,實(shí)例分析了ThinkPHP中U方法的使用技巧,需要的朋友可以參考下2015-06-06

