php驗(yàn)證碼實(shí)現(xiàn)代碼(3種)
驗(yàn)證碼在表單實(shí)現(xiàn)越來越多了,但是用js的寫的驗(yàn)證碼,總覺得不方便,所以學(xué)習(xí)了下php實(shí)現(xiàn)的驗(yàn)證碼。
好吧,其實(shí)是沒有事情干,但是又不想浪費(fèi)時(shí)間,所以學(xué)習(xí)了下php實(shí)現(xiàn)驗(yàn)證碼。正所謂,技多不壓身。而且,也可以封裝成一個(gè)函數(shù),以后使用的時(shí)候也是很方便的,當(dāng)然現(xiàn)在未封裝。
現(xiàn)在來說說簡(jiǎn)單的純數(shù)字驗(yàn)證碼吧。
如果是初學(xué)者,建議按照我代碼的注釋 //數(shù)字 一步步來。最簡(jiǎn)單的方法,還是把整個(gè)代碼復(fù)制走了。
新建一個(gè)captcha.php:
<?php //10>設(shè)置session,必須處于腳本最頂部 session_start(); $image = imagecreatetruecolor(100, 30); //1>設(shè)置驗(yàn)證碼圖片大小的函數(shù) //5>設(shè)置驗(yàn)證碼顏色 imagecolorallocate(int im, int red, int green, int blue); $bgcolor = imagecolorallocate($image,255,255,255); //#ffffff //6>區(qū)域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的區(qū)域著色,col 表示欲涂上的顏色 imagefill($image, 0, 0, $bgcolor); //10>設(shè)置變量 $captcha_code = ""; //7>生成隨機(jī)數(shù)字 for($i=0;$i<4;$i++){ //設(shè)置字體大小 $fontsize = 6; //設(shè)置字體顏色,隨機(jī)顏色 $fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深顏色 //設(shè)置數(shù)字 $fontcontent = rand(0,9); //10>.=連續(xù)定義變量 $captcha_code .= $fontcontent; //設(shè)置坐標(biāo) $x = ($i*100/4)+rand(5,10); $y = rand(5,10); imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); } //10>存到session $_SESSION['authcode'] = $captcha_code; //8>增加干擾元素,設(shè)置雪花點(diǎn) for($i=0;$i<200;$i++){ //設(shè)置點(diǎn)的顏色,50-200顏色比數(shù)字淺,不干擾閱讀 $pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200)); //imagesetpixel — 畫一個(gè)單一像素 imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor); } //9>增加干擾元素,設(shè)置橫線 for($i=0;$i<4;$i++){ //設(shè)置線的顏色 $linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //設(shè)置線,兩點(diǎn)一線 imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor); } //2>設(shè)置頭部,image/png header('Content-Type: image/png'); //3>imagepng() 建立png圖形函數(shù) imagepng($image); //4>imagedestroy() 結(jié)束圖形函數(shù) 銷毀$image imagedestroy($image);
接著就是靜態(tài)頁的代碼了:index.html
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>確認(rèn)驗(yàn)證碼</title> </head> <body> <form method="post" action="./form.php"> <p>驗(yàn)證碼: <img id="captcha_img" border='1' src='./captcha.php?r=<?php echo rand(); ?>' style="width:100px; height:30px" /> <a href="javascript:void(0)" onclick="document.getElementById('captcha_img').src='./captcha.php?r='+Math.random()">換一個(gè)?</a> </p> <P>請(qǐng)輸入驗(yàn)證碼:<input type="text" name='authcode' value=''/></p> <p><input type='submit' value='提交' style='padding:6px 5px;'/></p> </body> </html>
從index.html可以看到,提交的表單是到form.php的,所以還要有一個(gè)判斷的form.php代碼:
<?php header("Content-Type:text/html;charset=utf-8"); //設(shè)置頭部信息 //isset()檢測(cè)變量是否設(shè)置 if(isset($_REQUEST['authcode'])){ session_start(); //strtolower()小寫函數(shù) if(strtolower($_REQUEST['authcode'])== $_SESSION['authcode']){ //跳轉(zhuǎn)頁面 echo "<script language=\"javascript\">"; echo "document.location=\"./form.php\""; echo "</script>"; }else{ //提示以及跳轉(zhuǎn)頁面 echo "<script language=\"javascript\">"; echo "alert('輸入錯(cuò)誤!');"; echo "document.location=\"./form.php\""; echo "</script>"; } exit(); }
顯示頁面如下:
那么,純數(shù)字的實(shí)現(xiàn)了,數(shù)字加英文的也應(yīng)該不難了,廢話不多說了,拉代碼吧。
<?php //10>設(shè)置session,必須處于腳本最頂部 session_start(); $image = imagecreatetruecolor(100, 30); //1>設(shè)置驗(yàn)證碼圖片大小的函數(shù) //5>設(shè)置驗(yàn)證碼顏色 imagecolorallocate(int im, int red, int green, int blue); $bgcolor = imagecolorallocate($image,255,255,255); //#ffffff //6>區(qū)域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的區(qū)域著色,col 表示欲涂上的顏色 imagefill($image, 0, 0, $bgcolor); //10>設(shè)置變量 $captcha_code = ""; //7>生成隨機(jī)的字母和數(shù)字 for($i=0;$i<4;$i++){ //設(shè)置字體大小 $fontsize = 8; //設(shè)置字體顏色,隨機(jī)顏色 $fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深顏色 //設(shè)置需要隨機(jī)取的值,去掉容易出錯(cuò)的值如0和o $data ='abcdefghigkmnpqrstuvwxy3456789'; //取出值,字符串截取方法 strlen獲取字符串長(zhǎng)度 $fontcontent = substr($data, rand(0,strlen($data)),1); //10>.=連續(xù)定義變量 $captcha_code .= $fontcontent; //設(shè)置坐標(biāo) $x = ($i*100/4)+rand(5,10); $y = rand(5,10); imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); } //10>存到session $_SESSION['authcode'] = $captcha_code; //8>增加干擾元素,設(shè)置雪花點(diǎn) for($i=0;$i<200;$i++){ //設(shè)置點(diǎn)的顏色,50-200顏色比數(shù)字淺,不干擾閱讀 $pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200)); //imagesetpixel — 畫一個(gè)單一像素 imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor); } //9>增加干擾元素,設(shè)置橫線 for($i=0;$i<4;$i++){ //設(shè)置線的顏色 $linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //設(shè)置線,兩點(diǎn)一線 imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor); } //2>設(shè)置頭部,image/png header('Content-Type: image/png'); //3>imagepng() 建立png圖形函數(shù) imagepng($image); //4>imagedestroy() 結(jié)束圖形函數(shù) 銷毀$image imagedestroy($image);
其他的兩個(gè)頁面,不許要修改。
一般而言,現(xiàn)在就已經(jīng)夠用了。但是就像動(dòng)漫一樣,總會(huì)有番外。
那么,我們來個(gè)漢字的番外吧。其實(shí)我也準(zhǔn)備將漢字的驗(yàn)證碼放到我的畢業(yè)設(shè)計(jì)里面,雖然現(xiàn)在很流行滑動(dòng)驗(yàn)證碼,但是本人畢竟不是專門學(xué)習(xí)js的。
<?php //11>設(shè)置session,必須處于腳本最頂部 session_start(); //1>設(shè)置驗(yàn)證碼圖片大小的函數(shù) $image = imagecreatetruecolor(200, 60); //5>設(shè)置驗(yàn)證碼顏色 imagecolorallocate(int im, int red, int green, int blue); $bgcolor = imagecolorallocate($image,255,255,255); //#ffffff //6>區(qū)域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的區(qū)域著色,col 表示欲涂上的顏色 imagefill($image, 0, 0, $bgcolor); //7>設(shè)置ttf字體 $fontface = 'FZYTK.TTF'; //7>設(shè)置字庫,實(shí)現(xiàn)簡(jiǎn)單的數(shù)字儲(chǔ)備 $str='天地不仁以萬物為芻狗圣人不仁以百姓為芻狗這句經(jīng)常出現(xiàn)在控訴暴君暴政上地殘暴不仁把萬物都當(dāng)成低賤的豬狗來看待而那些高高在上的所謂圣人們也沒兩樣還不是把我們老百姓也當(dāng)成豬狗不如的東西但實(shí)在正取的解讀是地不情感用事對(duì)萬物一視同仁圣人不情感用事對(duì)百姓一視同仁執(zhí)子之手與子偕老當(dāng)男女主人公含情脈脈看著對(duì)方說了句執(zhí)子之手與子偕老女方淚眼朦朧含羞地回一句討厭啦這樣的情節(jié)我們是不是見過很多但是我們來看看這句的原句死生契闊與子成說執(zhí)子之手與子偕老于嗟闊兮不我活兮于嗟洵兮不我信兮意思是說戰(zhàn)士之間的約定說要一起死現(xiàn)在和我約定的人都走了我怎么活啊赤裸裸的兄弟江湖戰(zhàn)友友誼啊形容好基友的基情比男女之間的愛情要合適很多吧'; //str_split()切割字符串為一個(gè)數(shù)組,一個(gè)中文在utf_8為3個(gè)字符 $strdb = str_split($str,3); //>11 $captcha_code = ''; //8>生成隨機(jī)的漢子 for($i=0;$i<4;$i++){ //設(shè)置字體顏色,隨機(jī)顏色 $fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深顏色 //隨機(jī)選取中文 $in = rand(0,count($strdb)); $cn = $strdb[$in]; //將中文記錄到將保存到session的字符串中 $captcha_code .= $cn; /*imagettftext (resource $image ,float $size ,float $angle ,int $x ,int $y,int $color, string $fontfile ,string $text ) 幕布 ,尺寸,角度,坐標(biāo),顏色,字體路徑,文本字符串 mt_rand()生成更好的隨機(jī)數(shù),比rand()快四倍*/ imagettftext($image, mt_rand(20,24),mt_rand(-60,60),(40*$i+20),mt_rand(30,35),$fontcolor,$fontface,$cn); } //11>存到session $_SESSION['authcode'] = $captcha_code; //9>增加干擾元素,設(shè)置點(diǎn) for($i=0;$i<200;$i++){ //設(shè)置點(diǎn)的顏色,50-200顏色比數(shù)字淺,不干擾閱讀 $pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200)); //imagesetpixel — 畫一個(gè)單一像素 imagesetpixel($image, rand(1,199), rand(1,59), $pointcolor); } //10>增加干擾元素,設(shè)置線 for($i=0;$i<4;$i++){ //設(shè)置線的顏色 $linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //設(shè)置線,兩點(diǎn)一線 imageline($image,rand(1,199), rand(1,59),rand(1,199), rand(1,59),$linecolor); } //2>設(shè)置頭部,image/png header('Content-Type: image/png'); //3>imagepng() 建立png圖形函數(shù) imagepng($image); //4>imagedestroy() 結(jié)束圖形函數(shù) 銷毀$image imagedestroy($image);
其他的頁面也是不需要修改的。
效果圖如下:
以上就是php實(shí)現(xiàn)的三種驗(yàn)證碼:純數(shù)字驗(yàn)證碼,數(shù)字加英文驗(yàn)證碼,還有一種漢字驗(yàn)證碼,希望對(duì)大家熟練掌握php驗(yàn)證碼有所幫助。
- php+ajax注冊(cè)實(shí)時(shí)驗(yàn)證功能
- 基于PHP+Ajax實(shí)現(xiàn)表單驗(yàn)證的詳解
- PHP+Ajax異步通訊實(shí)現(xiàn)用戶名郵箱驗(yàn)證是否已注冊(cè)( 2種方法實(shí)現(xiàn))
- 教你php如何實(shí)現(xiàn)驗(yàn)證碼
- php發(fā)送短信驗(yàn)證碼完成注冊(cè)功能
- php生成4位數(shù)字驗(yàn)證碼的實(shí)現(xiàn)代碼
- php驗(yàn)證手機(jī)號(hào)碼
- php實(shí)現(xiàn)點(diǎn)擊可刷新驗(yàn)證碼
- 如何使用PHP對(duì)網(wǎng)站驗(yàn)證碼進(jìn)行破解
- PHP+Ajax實(shí)現(xiàn)驗(yàn)證碼的實(shí)時(shí)驗(yàn)證
相關(guān)文章
php微信公眾號(hào)開發(fā)之關(guān)鍵詞回復(fù)
這篇文章主要為大家詳細(xì)介紹了php微信公眾號(hào)開發(fā)之關(guān)鍵詞回復(fù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10PHP中set error handler函數(shù)用法小結(jié)
set_error_handler() 函數(shù)設(shè)置用戶自定義的錯(cuò)誤處理函數(shù)。該函數(shù)用于創(chuàng)建運(yùn)行時(shí)期間的用戶自己的錯(cuò)誤處理方法。該函數(shù)會(huì)返回舊的錯(cuò)誤處理程序,若失敗,則返回 null2015-11-11php頁面,mysql數(shù)據(jù)庫轉(zhuǎn)utf-8亂碼,utf-8編碼問題總結(jié)
一個(gè)網(wǎng)站如果需要國(guó)際化,就需要將編碼從GB2312轉(zhuǎn)成UTF-8,其中有很多的問題需要注意,如果沒有轉(zhuǎn)換徹底,將會(huì)有很多的編碼問題出現(xiàn)!接下來通過本篇文章給大家分享php頁面,mysql數(shù)據(jù)庫轉(zhuǎn)utf-8亂碼,utf-8編碼問題總結(jié),需要的朋友可以參考下2015-08-08PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)(oop)學(xué)習(xí)筆記(一) - 抽象類、對(duì)象接口、instanceof 和契約式編程
面向?qū)ο蟪绦蛟O(shè)計(jì)(OOP)是一種計(jì)算機(jī)編程架構(gòu)。OOP的一條基本原則是計(jì)算機(jī)程序是由單個(gè)能夠起到子程序作用的單元或?qū)ο蠼M合而成。OOP達(dá)到了軟件工程的三個(gè)主要目標(biāo):重用性、靈活性和擴(kuò)展性。為了實(shí)現(xiàn)整體運(yùn)算,每個(gè)對(duì)象都能夠接收信息、處理數(shù)據(jù)和向其它對(duì)象發(fā)送信息。2014-06-06laravel ORM 只開啟created_at的幾種方法總結(jié)
下面小編就為大家分享一篇laravel ORM 只開啟created_at的幾種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01