PHP如何?登入驗證碼校正功能

來源:互聯網
上載者:User
這篇文章主要為大家詳細介紹了PHP實現驗證碼校正功能,具有一定的參考價值,感興趣的小夥伴們可以參考一下

驗證碼的校正是利用PHP中的 SESSION功能來實現。
在最頂端聲明函數 session_start(); 告訴伺服器我們要用這個函數的功能。


session_start();


接下來我們用到的就是驗證碼實現的代碼。這裡用英文數位代碼為例。


$image = imagecreatetruecolor(100, 30); //建立一個100×30的畫布 $white = imagecolorallocate($image,255,255,255);//白色 imagefill($image,0,0,$white);//覆蓋黑色畫布


然後在驗證碼實現之前聲明一個空變數,用來存放驗證碼。


$session = ""; //空變數 ,存放驗證碼 for($i=0;$i<4;$i++){   $size = 6;   $x = $i*25+mt_rand(5,10);   $y = mt_rand(5,10);   $sizi_color = imagecolorallocate($image,mt_rand(80,220),mt_rand(80,220),mt_rand(80,220));   $char = join("",array_merge(range('a','z'),range('A','Z'),range(0,9)));   $char = str_shuffle($char);   $char = substr($char,0,1);   imagestring($image,$size,$x,$y,$char,$sizi_color);   $session .= $char ; //把驗證碼的每一個值賦值給變數 }   $_SESSION['session'] = $session; //這個變數的值與使用者輸入的值相等



for($k=0;$k<200;$k++){   $rand_color = imagecolorallocate($image,mt_rand(50,200),mt_rand(50,200),mt_rand(50,200));   imagesetpixel($image,mt_rand(1,99),mt_rand(1,29),$rand_color); }  for($n=0;$n<5;$n++){   $line_color = imagecolorallocate($image,mt_rand(80,220),mt_rand(80,220),mt_rand(80,220));   imageline($image,mt_rand(1,99),mt_rand(1,29),mt_rand(1,99),mt_rand(1,29),$line_color); }  header('content-type:image/png');//設定檔案輸出格式 imagepng( $image ); //以png格式輸出$image映像 imagedestroy( $image ); //銷毀映像


用 POST 方式來接收驗證碼。 strtolower 函數是讓伺服器不區分大小寫。這樣可以有效減少使用者的輸錯率。


if(isset($_POST['session'])){   session_start();   if(strtolower($_POST['session'])==strtolower($_SESSION['session'])){     echo'<font color="#0000CC">輸入正確</form>';   }else{     echo '<font color="#CC0000"><b>輸入錯誤</b></font>';   }   exit(); }


下面是HTML的頁面代碼。


<!DOCTYPE html> <html> <head>  <meta charset="utf-8"/>  <title>確認驗證碼</title> </head> <body>   <form method="post" action="./tushu.php">   <p>驗證碼圖片:<img id="img" border="1" src="http://localhost//xxx.php" width="100" height="30"></p>   <a href="javascript:void(0)" rel="external nofollow" onclick="document.getElementById('img').src='http://localhost//xxx.php'">看不清?換一個</a>   <p>請輸入圖片中的驗證碼:<input type="text" name="session" value=""/></p>   <p><input type="submit" value="提交" style="padding:6px 10px;"></p>   </form> </body> </html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.