驗證碼是什么:驗證碼是一種區分用戶是計算機還是人的公共程序
制作驗證碼需要四步
1:生成底圖
2:生成驗證內容
3:生成驗證碼內容
4:校驗驗證內容
先分步,第一步,生成底圖:
目標:通過php生成一張100*30大小的圖片
方法:imagecreatetruecolor($width,$height);
注意事項:依賴GD擴展
在輸出圖片前,必須提前輸出那張圖片的header 信息 --》》發送原生http頭
該方法默認輸出黑色背景
imagecreatetruecolor() 新建一個真彩色圖像 用$image來表示,之后,會大量用到
既然是創建真彩圖像,那就要有多樣的顏色 ,下面imagecolorallocate(選畫布,三色參數)
要用什么意思填充 imagefill(選畫布,開始位置 ,顏色)
致此,生成了底圖,下面開始加點作料
$image = imagecreatetruecolor(100,30)
$bgcolor = imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$bgcolor)
第二步:生成驗證內容
目標:隨機生成數字(大小,開始位置,內容,顏色)
方法:通過循環,imagestring 函數 ,水平的生成一行字符串(根據imagestring里面參數位置,往進填)
注意事項:控制好字體大小,N/n
for($i=0;$ i++){
此處根據imagestring里面的參數,定義變量,并且為變量賦值
imagestring($image,$fontsze,$x,$y,$fontcontent,$fontcolor)
}
$fontcontent = substr($data,rand(0,strlen($data)),1);
如果要數字和字母的組合,substr方法的意思是返回字符串的子串,返回的字符串隨機取得data,從這開始,最多有1個長度
第三步生成驗證碼 內容
目標:為驗證碼增加干擾元素,干擾元素為點或線
方法:imagesetpixel點,imageline-線(資源文件,起始位置,顏色)
注意事項:干擾元素一定要控制好顏色和數量,避免喧賓奪主
第四步:通過session存儲驗證信息
目標:在服務器端做記錄,便于用戶輸入驗證碼后做校驗
方法:session_start()
注意事項:session_start()必須處于腳本最頂端
多服務情況下,要考慮集中管理session管理
imagepng 以png格式將圖片輸出到瀏覽器或文件
imagedestroy 銷毀圖片 好習慣
在這些方法中,資源的使用非常多,就是每一個方法都要$image這個畫布
php?$image = imagecreatetruecolor( 100,30);$bgcolor = imagecolorallocate($image,255,255,255);imagefill($image,0,0,$bgcolor);// for($i=0;$i $i++){// $fontsize = 6;// $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));// $fontcontent = rand(0,9);// $x = ($i*100/4)+rand(5,10);// $y = rand(5,10);// imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor );$captch_code= for($i=0 ;$i $i++){ $fontsize = 6; $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120)); $data = abcdefhijkimnpqrstuvwxy345678 $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[ authcode ]=$captch_code;for($i=0;$i $i++){$pointcolor = imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));imagesetpixel($image,rand(1,99),rand(1,99),$pointcolor);for($i=0;$i $i++){$linecolor = imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220));imageline($image,rand(1,99),rand(1,99),rand(1,99),rand(1,99),$pointcolor);header( content-type: image/png imagepng( $image);//end;imagedestroy( $iamge);?
驗證碼的制作到這里就,接下來就是在服務器端,做校驗
src= captcha-2.php?r= ?php echo rand();? 對于這個r 找了資料,沒什么大用
image border= 1 src= captcha-2.php quot;this.src= captcha.php?t= + Math.random() title= 點擊刷新 / 本意是這樣
他這里還用t 呢,所以r 呀 t 呀
考慮到大小寫,這里使用strtolower() 將用戶輸入的大寫字母,統統轉化為小寫字母
?phpif(isset($_REQUEST[ code ])) session_start(); if (strtolower($_REQUEST[ code ])==$_SESSION[ code ]) header( Content-type: text/html; charset=UTF8 echo h1 color= #0000CC 輸入正確 /h1 else{ header( Content-type: text/html; charset=UTF8 echo h1 color= #CC0000 b 輸入錯誤 /b /h1 exit(); !DOCTYPE html html head meta charset= utf-8 / title 確認驗證 /title /head body form method= post action= form.php p 驗證碼圖片: img border= 1 src= captcha-2.php?r= ?php echo rand();? width= 100 height= 30 p 請輸入圖片的內容: input type= text name= code value= / /p p input type= submit value= 提交 >相關文章推薦:
如何用PHP將txt文件內容轉換成數組并按行數獲取指定內容(示例)
php如何利用經度和緯度來計算兩點之間的距離(純代碼)
php如何刪除目錄及目錄下的所有文件的代碼實例
以上就是php實現驗證碼的步驟以及服務端校驗的代碼的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答