PHP通過GD庫(kù)實(shí)現(xiàn)驗(yàn)證碼功能示例
本文實(shí)例講述了PHP通過GD庫(kù)實(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):
<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ù)。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP5中GD庫(kù)生成圖形驗(yàn)證碼(有漢字)
- php中使用GD庫(kù)做驗(yàn)證碼
- php通過GD庫(kù)實(shí)現(xiàn)驗(yàn)證碼功能
- PHP使用GD庫(kù)制作驗(yàn)證碼的方法(點(diǎn)擊驗(yàn)證碼或看不清會(huì)刷新驗(yàn)證碼)
- PHP 用session與gd庫(kù)實(shí)現(xiàn)簡(jiǎn)單驗(yàn)證碼生成與驗(yàn)證的類方法
- php結(jié)合GD庫(kù)簡(jiǎn)單實(shí)現(xiàn)驗(yàn)證碼的示例代碼
- php結(jié)合GD庫(kù)實(shí)現(xiàn)中文驗(yàn)證碼的簡(jiǎn)單方法
相關(guān)文章
php自定義時(shí)間轉(zhuǎn)換函數(shù)示例
這篇文章主要介紹了php自定義時(shí)間轉(zhuǎn)換函數(shù),可實(shí)現(xiàn)時(shí)間戳與字符串時(shí)間顯示格式之間的轉(zhuǎn)換功能,需要的朋友可以參考下2016-12-12PHP使用pcntl_fork實(shí)現(xiàn)多進(jìn)程下載圖片的方法
這篇文章主要介紹了PHP使用pcntl_fork實(shí)現(xiàn)多進(jìn)程下載圖片的方法,較為詳細(xì)的分析了pcntl_fork的原理與用法,以及使用pcntl_fork實(shí)現(xiàn)多進(jìn)程下載圖片的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12PHP函數(shù)checkdnsrr用法詳解(Windows平臺(tái)用法)
這篇文章主要介紹了PHP函數(shù)checkdnsrr用法,分析講解了在Windows平臺(tái)使用checkdnsrr函數(shù)的方法,需要的朋友可以參考下2016-03-03PHP調(diào)用Linux的命令行執(zhí)行文件壓縮命令
一開始,我和普通青年一樣,想到用PHP內(nèi)置的 ZipArchive糾結(jié)的是環(huán)境上沒安裝zip擴(kuò)展,想采用用PHP調(diào)用Linux的命令行 ,執(zhí)行壓縮命令,感興趣的朋友可以了解下,希望本文對(duì)你有所幫助2013-01-01golang、python、php、c++、c、java、Nodejs性能對(duì)比
這篇文章主要介紹了golang、python、php、c++、c、java、Nodejs性能對(duì)比的相關(guān)資料,需要的朋友可以參考下2017-03-03