当前位置:首页 > PHP教程 > php函数 > 列表

php生成带干扰的验证码程序

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-23 14:56:14 浏览: 评论:0 

这里介绍了一款安全性比较高的验证生成程序,可以带干扰线等内容,可以有效的防止用户用程序识别验证码的难度了,代码如下:

  1. <?php 
  2. /* 
  3.  * Created on 2011-3-11 
  4.  * Programmer : xiaoyao, QQ:1045195056 
  5.  验证通过判断输入值与$_SESSION['check_pic']值 
  6.  */ 
  7. session_start(); 
  8.  function RandAscii($number){//$number产生数字和字母个数 
  9. $arr=array('0','1','2','3','4','5','6','7','8','9', 
  10. 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','w','v','u','x','y','z'); 
  11. for ($i=1;$i<=$number;$i++) 
  12. { 
  13. $rand= $rand.$arr[rand(0,35)]; 
  14. } 
  15. return $rand; 
  16. } 
  17.  $rand=RandAscii(4); 
  18.  
  19. $_SESSION['check_pic']=$rand;//随机产生的四个数赋值session中,用于验证。 
  20. $x=80; 
  21. $y=24; 
  22.  $im=imagecreatetruecolor($x,$y);//创建图片 
  23. $bg=imagecolorallocate($im,255,255,255);//设置颜色背景 
  24. imagefill( $im,0,0,$bg); 
  25. $wh=imagecolorallocate($im,255,255,0); 
  26. $grey=imagecolorallocate($im,128,128,128); 
  27. $yellow=imagecolorallocate($im,255,255,0); 
  28. $red=imagecolorallocate($im,0,255,0); 
  29. $foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)), 
  30.   imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)), 
  31.   imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)), 
  32.   imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255)) 
  33.  );//字颜色数组 
  34.  //画边框 
  35.  $border = imagecolorallocate($im, 133, 153, 193); 
  36.  imagerectangle($im, 0, 0, $x - 1, $y - 1, $border); 
  37.  
  38. for($i=0;$i<10;$i++){        //画干扰线,10条 
  39. imageline($im,rand(0,60),2,rand(0,60),20,$yellow); 
  40.  
  41. } 
  42. for($j=0;$j<100;$j++){ 
  43.  imagesetpixel($im,rand()%76,rand()%20,$red); 
  44. } 
  45. //imagestring($im,6,15,8,$rand,$wh);//字体大小1-5 
  46. imagettftext($im, 14,rand(30, -30), 5, rand(15, 18) ,$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[0]); 
  47. imagettftext($im, 14,rand(50, -50), 24, rand(15, 18),$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[1]); 
  48. imagettftext($im, 14,rand(50, -50), 43, rand(15, 18) ,$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[2]); 
  49. imagettftext($im, 14,rand(30, -30), 62, rand(15, 18),$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[3]); 
  50. //开源代码phpfensi.com 
  51. header("Content-type: image/jpeg");//输出图片 
  52. imagejpeg($im); 
  53. imagedestroy($im); 
  54. ?> 

调用方法,代码如下:

  1. <?php 
  2. /* 
  3.  * Created on 2011-3-11 
  4.  * Programmer : xiaoyao, QQ:1045195056 
  5.  验证通过判断输入值与$_SESSION['check_pic']值 
  6.  */ 
  7. session_start();//开启session 
  8. if(isset($_POST['check'])) 
  9. { 
  10. if($_POST['check']) 
  11.  { 
  12. if($_POST['check']==$_SESSION['check_pic']) 
  13.  { 
  14.  echo " 验证码正确".$_SESSION['check_pic']; 
  15.  } 
  16. else 
  17.  { 
  18.  echo " 验证码错误".$_SESSION['check_pic']; 
  19.  } 
  20. } 
  21. } 
  22. ?> 
  23. <FORM METHOD=POST ACTION=""> 
  24. <img src="index.php"><br>    <!----链接图片---> 
  25. <input type="text" name="check" > 
  26. <input type="submit" value="提交"> 
  27. </FORM> 

Tags: php生成验证码 php验证码类

分享到:

相关文章