php實現可運算的驗證碼_php技巧

來源:互聯網
上載者:User

本文分享了php實現可運算的驗證碼的代碼執行個體,希望對大家的學習有所協助。

demo.php代碼

<?php session_start(); if(!emptyempty($_POST['Login'])){   if(md5(strtoupper($_POST['Login'])) == $_SESSION['Login']){     echo 'Correct';   }else{     echo 'Error';     } } ?>  <form method="post">   <input type="text" name="Login" value="" /><img src="img.php?Action=Login&imgW=80&imgH=30" align="absmiddle">   <input type="submit" value="Login" /> </form> 

img.php代碼

<?php session_start();  /** * 隨機的數字,之和驗證碼 * 修改日期 2006-12-20 */ function getCode ($length = 32, $mode = 0) { switch ($mode) {    case '1':      $str = '123456789';       break;  case '2':       $str = 'abcdefghijklmnopqrstuvwxyz';       break;  case '3':       $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';       break;  case '4':       $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';       break;  case '5':       $str = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';       break;     case '6':       $str = 'abcdefghijklmnopqrstuvwxyz1234567890';       break;     default:       $str = 'ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789';       break; }  $result = ''; $l = strlen($str)-1; for($i = 0;$i < $length;$i ++) {  $num = rand(0, $l);  $result .= $str[$num]; } return $result; } //建立驗證圖片 function createAuthNumImg($randStr,$imgW=100,$imgH=40,$fontName) { header ("content-type: image/png"); $image = imagecreate($imgW , $imgH); $color_white = imagecolorallocate($image , 255 , 255 , 255); $color_gray = imagecolorallocate($image , 228 , 228 , 228); $color_black = imagecolorallocate($image , 255 , 102 , 204); for ($i = 0 ; $i < 1000 ; $i++) {  imagesetpixel($image , mt_rand(0 , $imgW) , mt_rand(0 , $imgH) , $color_gray); } imagerectangle($image , 0 , 0 , $imgW - 1 , $imgH - 1 , $color_gray); for ($i=10;$i<$imgH;$i+=10)  imageline($image, 0, $i, $imgW, $i, $color_gray); imagettftext($image,16,5,3,25,$color_black,$fontName,$randStr); for ($i=10;$i<$imgW;$i+=10)  imageline($image, $i, 0, $i, $imgH, $color_gray); imagepng($image); imagedestroy($image); }  $a=GetCode(1,1); $b=GetCode(1,1); $c=GetCode(1,1); $Passport=$a."+".$b."+".$c; $Total=$a+$b+$c; $Total; $_SESSION[$_GET['Action']]=md5(strtoupper($Total)); createAuthNumImg($Passport,$_GET['imgW'],$_GET['imgH'],"verdana.ttf"); ?> 

以上就是為大家介紹的php可運算的驗證碼全部代碼,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.