服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > php教程 >

php 验证码详细生成与使用方法

时间:2016-05-11 12:42来源: 作者: 点击:
注意:以下代码需要打开php的gd库,修改php.in文件的配置,把已经注释掉的行之前的分号取消即可:extension=php_gd2.dll. 实例一,代码如下: $width =165; $height =120; $image =imagecreatetruecolor( $width , $hei

注意:以下代码需要打开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. //开源代码最模板zuimoban.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. ?> 
  30.  
(责任编辑:最模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容