国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

一個簡單的php驗證碼程序

2024-05-04 23:06:25
字體:
來源:轉載
供稿:網友

本文章介紹一下關于在php中驗證碼程序的生成代碼,調用方法及以如果驗證用戶輸入的驗證碼程序沒有問題.

create_code.php,代碼如下:

  1. <?php 
  2. session_start(); 
  3. //生成驗證碼圖片 
  4. header("Content-type: image/png"); 
  5. // 全數字 
  6. $str = "1,2,3,4,5,6,7,8,9,a,b,c,d,f,g";    //要顯示的字符,可自己進行增刪 
  7. $list = explode(","$str); 
  8. $cmax = count($list) - 1; 
  9. $verifyCode = ''
  10. for ( $i=0; $i < 5; $i++ ){ 
  11.       $randnum = mt_rand(0, $cmax); 
  12.       $verifyCode .= $list[$randnum];  //取出字符,組合成為我們要的驗證碼字符 
  13. $_SESSION['code'] = $verifyCode;        //將字符放入SESSION中 
  14.  
  15. $im = imagecreate(58,28);    //生成圖片 
  16. $black = imagecolorallocate($im, 0,0,0);     //此條及以下三條為設置的顏色 
  17. $white = imagecolorallocate($im, 255,255,255); 
  18. $gray = imagecolorallocate($im, 200,200,200); 
  19. $red = imagecolorallocate($im, 255, 0, 0); 
  20. imagefill($im,0,0,$white);     //給圖片填充顏色 
  21.  
  22. //將驗證碼繪入圖片 
  23. imagestring($im, 5, 10, 8, $verifyCode$black);    //將驗證碼寫入到圖片中 
  24.  
  25. for($i=0;$i<50;$i++)  //加入干擾象素 
  26.      imagesetpixel($im, rand()p , rand()0 , $black);    //加入點狀干擾素 
  27.      imagesetpixel($im, rand()p , rand()0 , $red); 
  28.      imagesetpixel($im, rand()p , rand()0 , $gray); 
  29.      //imagearc($im, rand()p, rand()p, 20, 20, 75, 170, $black);    //加入弧線狀干擾素 
  30.      //imageline($im, rand()p, rand()p, rand()p, rand()p, $red);    //加入線條狀干擾素 
  31. imagepng($im); 
  32. imagedestroy($im); 
  33. ?>  

html代碼,demo.html代碼如下:

  1. <!-- DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd" --> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5.     <title></title> 
  6. </head> 
  7.  
  8. <body> 
  9. <form action="act.php" method="post"> 
  10. <input type="text" name="code" /> 
  11. <img id="code" src="create_code.php" alt="看不清楚,換一張" style="cursor: pointer; vertical-align:middle;" onClick="create_code()"/> 
  12. <!--<button type="button" onClick="create_code()">更換</button>--> 
  13. <button type="submit">提交</button> 
  14. </form> 
  15. <script> 
  16. function create_code(){ 
  17.     document.getElementByIdx_x('code').src = 'create_code.php?'+Math.random()*10000; 
  18. </script> 
  19. </body> 
  20. </html> 

處理,判斷是否輸入正確 act.php,代碼如下:

  1. <?php 
  2. session_start(); 
  3. if($_POST['code'] == $_SESSION['code']){ 
  4.     echo 'ok'
  5. }else
  6.     echo 'no'
  7. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 深圳市| 福贡县| 五寨县| 祁阳县| 神木县| 长海县| 九寨沟县| 惠来县| 通化市| 虹口区| 永兴县| 潮州市| 建德市| 濉溪县| 龙游县| 长丰县| 新巴尔虎右旗| 济南市| 九江县| 承德市| 白银市| 榆社县| 隆德县| 广河县| 上饶县| 石泉县| 定结县| 巴彦淖尔市| 太仓市| 呼图壁县| 和顺县| 石景山区| 隆林| 铁岭市| 左权县| 台南县| 浦东新区| 武穴市| 阳曲县| 桦南县| 灌南县|