標籤:工作 action view method normal bst 點擊 div pac
1.根據php中的GD庫對圖片進行處理,繪製出驗證碼的圖片,code.php中
2.表單介面,簡單的session儲存及與使用者輸入對比,確定是否驗證正確,form.php中
<?php session_start();//必須位於指令碼的最頂端 $image=imagecreatetruecolor(100, 30);//imagecreatetruecolor函數建一個真彩色映像 //產生彩色像素 $bgcolor=imagecolorallocate($image, 255, 255, 255);//白色背景 imagecolorallocate函數為一幅映像分配顏色 $textcolor=imagecolorallocate($image,0,0,255);//藍色文本 //填充函數,xy確定座標,color顏色執列區域填充顏色 imagefill($image, 0, 0, $bgcolor); $captch_code="";//初始空值 //該迴圈,迴圈取數 for($i=0;$i<4;$i++){ $fontsize=6; $x=($i*25)+rand(5,10); $y=rand(5,10);//位置隨機 // $fontcontent=$i>2?chr(rand(97,122)):chr(rand(65,90));//是小寫,否則是大寫 $data=‘abcdefghijkmnpqrstuvwxyz3456789‘; $fontcontent=substr($data,rand(0,strlen($data)-1),1);//strlen僅僅是一個計數器的工作 含數字和字母的驗證碼 //可以理解為數組長度0到30
$fontcolor=imagecolorallocate($image,rand(0,100),rand(0,100),rand(0,100));//隨機的rgb()值可以自己定
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); //水平地畫一行字串 $captch_code.=$fontcontent;} $_SESSION[‘authcode‘]=$captch_code;//將變數儲存再session的authcode變數中 //該迴圈,迴圈畫背景幹擾的點 for($m=0;$m<=600;$m++){ $x2=rand(1,99); $y2=rand(1,99); $pointcolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255)); imagesetpixel($image,$x2,$y2,$pointcolor);// 水平地畫一串像素點 } //該迴圈,迴圈畫幹擾直線 for ($i=0;$i<=10;$i++){ $x1=rand(0,99); $y1=rand(0,99); $x2=rand(0,99); $y2=rand(0,99); $linecolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255)); imageline($image,$x1,$y1,$x2,$y2,$linecolor);//畫一條線段 } header(‘content-type:image/png‘); imagepng($image); //銷毀 imagedestroy($image);?>
<--------------------------------code.php---------------------------結束----------------------------------------->
<?php// session 存值並匹配使用者輸入值 if (isset($_REQUEST[‘authcode‘])) { session_start(); if (strtolower($_REQUEST[‘authcode‘])==$_SESSION[‘authcode‘]) {//strtolower轉化為小寫函數 echo"輸入正確!"; # code... } else{ echo"輸入錯誤!"; } exit(); }?><!DOCTYPE html><html lang="en"><head><!-- 簡單的表單提交代碼 --> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>簡單驗證碼的實現</title></head><body> <form method="post" action="./form.php"> <p>驗證碼圖片:<img src="code.php" onClick="this.src=‘code.php?nocache=‘+Math.random()" style="cursor:hand" alt="點擊換一張"/>點擊圖片可更換驗證碼</p> <p>請輸入圖片中的內容:<input type="text" name="authcode" value=""/></p> <p><input type="submit" width="20px" height=19px value="提交"></input></p> </form> </body></html>
<---------------------------------form.php 表單介面---------------結束---------------------------------------------->
php實現簡單的驗證碼功能