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

首頁 > 語言 > PHP > 正文

php 驗證碼詳細生成與使用方法

2024-09-04 11:44:28
字體:
來源:轉載
供稿:網友

注意:以下代碼需要打開php的gd庫,修改php.in文件的配置,把已經注釋掉的行之前的分號取消即可:extension=php_gd2.dll.

實例一,代碼如下:

  1. $width = 165; 
  2.         $height = 120; 
  3.         $image = imagecreatetruecolor($width,$height); 
  4.         $bg_color = imagecolorallocate($image,255,255,255); 
  5.         $tm = imagecolorallocate($image,255,0,0); 
  6.         imagefilledrectangle($image,0,0,$width,$height,$bg_color); 
  7.         imagefill($image,0,0,$bg_color); 
  8.         /* 
  9.         //$text = random_text(5); 
  10.         $text = "ffff"; 
  11.         $font = 8; 
  12.         $struft=iconv("gbk","utf-8",$text); 
  13.         $x = imagesx($image); 
  14.         $y = imagesy($image); 
  15.         $fg_color = imagecolorallocate($image,233,14,91); 
  16.         imagestring($image,$font,$x,$y,$struft,$fg_color); 
  17.         $_session['captcha'] = $text; 
  18.         */ 
  19. header("content-type:image/png"); 
  20.         imagepng($image); 
  21.         imagedestroy($image); 

實例二,代碼如下:validate.php

采用了session識別,稍微改進了一下目前網絡上流傳的php驗證碼,加入雜點,數字顏色隨機顯示,控制4位數字顯示.

  1. <?php 
  2. session_start(); 
  3. //生成驗證碼圖片 
  4. header("content-type: image/png"); 
  5. $im = imagecreate(44,18); 
  6. $back = imagecolorallocate($im, 245,245,245); 
  7. imagefill($im,0,0,$back); //背景 
  8.  
  9. srand((double)microtime()*1000000); 
  10. //生成4位數字 
  11. for($i=0;$i<4;$i++){ 
  12. $font = imagecolorallocate($im, rand(100,255),rand(0,100),rand(100,255)); 
  13. $authnum=rand(1,9); 
  14. $vcodes.=$authnum
  15. imagestring($im, 5, 2+$i*10, 1, $authnum$font); 
  16.  
  17. for($i=0;$i<100;$i++) //加入干擾象素 
  18. //開源代碼Vevb.com 
  19. $randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); 
  20. imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); 
  21. }  
  22. imagepng($im); 
  23. imagedestroy($im); 
  24.  
  25. $_session['vcode'] = $vcodes
  26. ?> 

下面看完整實例,代碼如下:

  1. <?php 
  2.  
  3.     @header("content-type:text/html; charset=utf-8"); 
  4.  
  5.     //打開session 
  6.  
  7.     session_start(); 
  8.  
  9. ?> 
  10.  
  11. <html> 
  12.  
  13.     <head> 
  14.  
  15.        <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
  16.  
  17.        <title>php驗證碼示例</title> 
  18.  
  19.     </head> 
  20.  
  21.     <body> 
  22.  
  23.        驗證碼:<br/> 
  24.  
  25.        <iframe id="iimg" height="100" width=300 src="img.php" frameborder="0" ></iframe> 
  26.  
  27.        <br/> 
  28.  
  29.        <input type=button value="看不清,換一張" onclick="iimg.location.reload();"
  30.  
  31.        <br> 
  32.  
  33.        <form action="validate.php" method="post"
  34.  
  35.            輸入驗證碼:<input name="imgid" style="width:60"
  36.  
  37.            <input type="submit" value="確定"
  38.  
  39.        </form> 
  40.  
  41.     </body> 
  42.  
  43. </html> 

php判斷用戶輸入的驗證碼是否與系統生成的一致,代碼如下:

  1. <?php @header("content-type:text/html; charset=utf-8"); 
  2.  
  3.     //開啟session 
  4.  
  5.     session_start(); 
  6.  
  7.     //得到用戶輸入的驗證碼,并轉換成大寫 
  8.  
  9.     $imgid_req = $_request['imgid']; 
  10.  
  11.     $imgid_req = strtoupper($imgid_req); 
  12.  
  13.     //驗證該字符串是否注冊了session變量 
  14.  
  15.     if (session_is_registered($imgid_req)) { 
  16.  
  17.        echo "<font color=blue >通過驗證!</font>"
  18.  
  19.     } else { 
  20.  
  21.        echo "<font color=red >驗證錯誤!</font>"
  22.  
  23.     } 
  24.  
  25.     //關閉session,以清除所有注冊過的變量 
  26.  
  27.     session_destroy(); 
  28.  
  29. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 历史| 衡水市| 徐闻县| 西安市| 宁南县| 龙南县| 平乐县| 綦江县| 子洲县| 芒康县| 甘德县| 象州县| 阳西县| 隆回县| 新建县| 固原市| 宜城市| 临汾市| 炎陵县| 教育| 余姚市| 天津市| 莲花县| 澄江县| 准格尔旗| 开平市| 太白县| 岐山县| 留坝县| 历史| 灵璧县| 苍山县| 永城市| 马关县| 砚山县| 东山县| 澳门| 乐昌市| 通许县| 怀来县| 安岳县|