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

具體實(shí)現(xiàn):
<?php/*PHP實(shí)現(xiàn)驗(yàn)證碼*/
session_start();//開啟會(huì)話
//創(chuàng)建畫布
$image=imagecreatetruecolor(100,38);
//背景顏色
$bgcolor=imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$bgcolor);
$captch_code='';//存儲(chǔ)驗(yàn)證碼
//隨機(jī)選取4個(gè)數(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)表單的連接和“換一個(gè)”功能的實(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()">換一個(gè)</a> </label>
captcha.php后面加上一個(gè)隨機(jī)參數(shù)是為了實(shí)現(xiàn)換一個(gè)驗(yàn)證碼的功能,,換一個(gè)功能通過簡(jiǎn)單的js即可實(shí)現(xiàn)。如果沒有這個(gè)功能可以不用參數(shù)。
總結(jié)
以上所述是小編給大家介紹的php通過GD庫實(shí)現(xiàn)驗(yàn)證碼功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
php強(qiáng)制文件下載而非在瀏覽器打開的自定義函數(shù)分享
這篇文章主要介紹了php強(qiáng)制文件下載而非在瀏覽器打開的自定義函數(shù)分享,需要的朋友可以參考下2014-05-05
Thinkphp 5.0實(shí)現(xiàn)微信企業(yè)付款到零錢
這篇文章主要為大家詳細(xì)介紹了Thinkphp 5.0實(shí)現(xiàn)微信企業(yè)付款到零錢,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
laravel-admin 管理平臺(tái)獲取當(dāng)前登陸用戶信息的例子
今天小編就為大家分享一篇laravel-admin 管理平臺(tái)獲取當(dāng)前登陸用戶信息的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10
Codeigniter框架實(shí)現(xiàn)獲取分頁數(shù)據(jù)和總條數(shù)的方法
這篇文章主要介紹了Codeigniter框架實(shí)現(xiàn)獲取分頁數(shù)據(jù)和總條數(shù)的方法,實(shí)現(xiàn)了對(duì)獲取當(dāng)前頁的數(shù)據(jù)和總條數(shù)方法的封裝,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12
PHP設(shè)計(jì)模式(三)建造者模式Builder實(shí)例詳解【創(chuàng)建型】
這篇文章主要介紹了PHP設(shè)計(jì)模式:建造者模式Builder,結(jié)合實(shí)例形式詳解分析了PHP建造者模式Builder相關(guān)概念、功能、原理、實(shí)現(xiàn)技巧與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-05-05
從零開始學(xué)YII2框架(三)擴(kuò)展插件yii2-gird
yii2-gird 插件是Yii2.0的一個(gè)擴(kuò)展。它在官方的girdview基礎(chǔ)上擴(kuò)展了一些實(shí)用的功能。非常好用,推薦使用哦。2014-08-08
Zend Framework動(dòng)作助手Url用法詳解
這篇文章主要介紹了Zend Framework動(dòng)作助手Url用法,結(jié)合實(shí)例形式分析了動(dòng)作助手Url的功能,定義與相關(guān)使用技巧,需要的朋友可以參考下2016-03-03

