站長資訊網(wǎng)
最全最豐富的資訊網(wǎng)站

php如何實現(xiàn)獲取驗證碼

php實現(xiàn)獲取驗證碼的方法:【<?php session_start();$image = imagecreatetruecolor(100, 30);imagecolorallocate(int im, int red, int…】。

php如何實現(xiàn)獲取驗證碼

本文操作環(huán)境:windows10系統(tǒng)、php 7、thinkpad t480電腦。

在日常生活中我們會經(jīng)常使用到驗證碼功能,那么如果我們要自己實現(xiàn)一個驗證碼功能該如何去做呢?下面我們給出具體的實現(xiàn)代碼,供大家參考!

如果你是一名初學(xué)者,那么我強烈建議你跟著代碼中的注釋一步步來,而不是直接復(fù)制代碼。

新建一個captcha.php:

<?php   //11>設(shè)置session,必須處于腳本最頂部   session_start();        /*$image = imagecreatetruecolor(100, 30);    //1>設(shè)置驗證碼圖片大小的函數(shù)   //5>設(shè)置驗證碼顏色 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>生成隨機數(shù)字   for($i=0;$i<4;$i++){     //設(shè)置字體大小     $fontsize = 6;         //設(shè)置字體顏色,隨機顏色     $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è)置雪花點   for($i=0;$i<200;$i++){     //設(shè)置點的顏色,50-200顏色比數(shù)字淺,不干擾閱讀     $pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));         //imagesetpixel — 畫一個單一像素     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è)置線,兩點一線     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 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標(biāo)題文檔</title> </head>  <body>  <form method="post" action="./form.php">   <p>驗證碼: <img id="captcha_img" border='1' src='./captcha.php?r=echo rand(); ?>' style="width:100px; height:30px" />     <a href="javascript:void(0)" onclick="document.getElementById('captcha_img').src='./captcha.php?r='+Math.random()">換一個?</a>   </p>   <P>請輸入驗證碼:<input type="text" name='authcode' value=''/></p>   <p><input type='submit' value='提交' style='padding:6px 5px;'/></p> </form>  </body> </html>

從index.html可以看到,提交的表單是到form.php的,所以還要有一個判斷的form.php代碼:

<?php   header("Content-Type:text/html;charset=utf-8");      //設(shè)置頭部信息   //isset()檢測變量是否設(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('輸入錯誤!');";       echo "document.location="./form.php"";       echo "</script>";     }     exit();   }

顯示頁面如下:

php如何實現(xiàn)獲取驗證碼

數(shù)字加英文的驗證碼,只需更改captcha.php頁面中的 7》即可,其他兩個頁面不需要動,代碼如下:

<?php   //11>設(shè)置session,必須處于腳本最頂部   session_start();   $image = imagecreatetruecolor(100, 30);    //1>設(shè)置驗證碼圖片大小的函數(shù)   //5>設(shè)置驗證碼顏色 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>生成隨機的字母和數(shù)字   for($i=0;$i<4;$i++){     //設(shè)置字體大小     $fontsize = 8;         //設(shè)置字體顏色,隨機顏色     $fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120));      //0-120深顏色     //設(shè)置需要隨機取的值,去掉容易出錯的值如0和o     $data ='abcdefghigkmnpqrstuvwxy3456789';     //取出值,字符串截取方法  strlen獲取字符串長度     $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è)置雪花點   for($i=0;$i<200;$i++){     //設(shè)置點的顏色,50-200顏色比數(shù)字淺,不干擾閱讀     $pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));         //imagesetpixel — 畫一個單一像素     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è)置線,兩點一線     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);

顯示頁面如下:

php如何實現(xiàn)獲取驗證碼

生成漢字類驗證碼,在運行過程中,提示亂碼錯誤,無法顯示,未能解決,代碼如下:

php   //11>設(shè)置session,必須處于腳本最頂部   session_start();     //1>設(shè)置驗證碼圖片大小的函數(shù)   $image = imagecreatetruecolor(200, 60);       //5>設(shè)置驗證碼顏色 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è)置字庫,實現(xiàn)簡單的數(shù)字儲備   $str='天地不仁以萬物為芻狗圣人不仁以百姓為芻狗這句經(jīng)常出現(xiàn)在控訴暴君暴政上地殘暴不仁把萬物都當(dāng)成低賤的豬狗來看待而那些高高在上的所謂圣人們也沒兩樣還不是把我們老百姓也當(dāng)成豬狗不如的東西但實在正取的解讀是地不情感用事對萬物一視同仁圣人不情感用事對百姓一視同仁執(zhí)子之手與子偕老當(dāng)男女主人公含情脈脈看著對方說了句執(zhí)子之手與子偕老女方淚眼朦朧含羞地回一句討厭啦這樣的情節(jié)我們是不是見過很多但是我們來看看這句的原句死生契闊與子成說執(zhí)子之手與子偕老于嗟闊兮不我活兮于嗟洵兮不我信兮意思是說戰(zhàn)士之間的約定說要一起死現(xiàn)在和我約定的人都走了我怎么活啊赤裸裸的兄弟江湖戰(zhàn)友友誼啊形容好基友的基情比男女之間的愛情要合適很多吧';   //str_split()切割字符串為一個數(shù)組,一個中文在utf_8為3個字符   $strdb = str_split($str,3);     //>11   $captcha_code = '';   //8>生成隨機的漢子   for($i=0;$i<4;$i++){     //設(shè)置字體顏色,隨機顏色     $fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120));      //0-120深顏色     //隨機選取中文     $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()生成更好的隨機數(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è)置點   for($i=0;$i<200;$i++){     //設(shè)置點的顏色,50-200顏色比數(shù)字淺,不干擾閱讀     $pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));         //imagesetpixel — 畫一個單一像素     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è)置線,兩點一線     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);

推薦學(xué)習(xí):php培訓(xùn)

贊(0)
分享到: 更多 (0)
網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號
伊人精品久久久大香线蕉99| 日韩精品内射视频免费观看| 精品国产乱码久久久久久人妻| 中文日韩亚洲欧美制服| 亚洲AV无码精品色午夜果冻不卡| 日韩精品一区二区三区老鸦窝| 免费视频精品一区二区三区| 国产精品电影一区二区三区| 久久亚洲精品成人| 日韩在线观看第一页| 亚洲午夜精品久久久久久app| 久久se精品一区精品二区| 国产亚洲精品欧洲在线观看| 99re热久久精品这里都是精品| 精品一区二区三区中文| 2021国产精品成人免费视频| 最新在线精品国自av| 国产精品亚洲а∨无码播放麻豆| 熟妇人妻VA精品中文字幕| 国产精品热久久毛片| 国产乱人伦偷精品视频下| 国产精品成人观看视频国产| 亚洲一区爱区精品无码| 色老成人精品视频在线观看| 美利坚永久精品视频在线观看| 精品无码国产AV一区二区三区| 久久精品7亚洲午夜a| 国产精品一区二区三区99 | 国产精品亚洲精品久久精品 | 奇米影视7777久久精品人人爽| 亚洲AV日韩精品久久久久久 | 成人综合久久精品色婷婷| 99精品视频免费观看| 国产在线精品一区二区三区直播| 亚洲日韩一区二区一无码| 日韩精品一区二区三区毛片| 琪琪精品视频在线观看| 日韩精品免费一级视频| 人妻少妇精品视频二区| 亚洲一区精品无码| 91精品国产福利尤物|