php图片验证码代码

在用户验证页面,如注册,登录的时候,为了加强用户登录的安全性,添加验证码验证代码,下面我为各位朋友提供几种不同同类型的php验证代码程序,最后一款是一款使用了验证代码的实例代码,实例代码如下:

  1. date_default_timezone_set('asia/shanghai');
  2. function setcode($len)
  3. {
  4. $code = '';
  5. for ($i=0;$i<$len;$i++)//生成随机长度
  6. {
  7. $code .= chr(drand());
  8. }
  9. return $code;
  10. }
  11. //生成一个随机字符
  12. function drand()
  13. {
  14. $rand = mt_rand(0,2);
  15. $str = '';
  16. switch ($rand)
  17. {
  18. case 0: $str = mt_rand(48,57);break;//数字
  19. case 1: $str = mt_rand(65,90);break;//大写字母
  20. case 2: $str = mt_rand(97,122);break;//小写字母
  21. }
  22. return $str;
  23. }
  24. $_session['checkcode'] = $code = setcode(5);
  25. //php图片验证码原代码,需支持gd2.dll扩展,需修改php.ini后重启iis or ap
  26. session_start();
  27. session_register('safecode');
  28. $type = 'gif';
  29. $width= 40;
  30. $height= 16;
  31. header("content-type: image/".$type);
  32. srand((double)microtime()*1000000);
  33. $randval = randstr(4,"");
  34. if($type!='gif' && function_exists('imagecreatetruecolor')){
  35. $im = @imagecreatetruecolor($width,$height);
  36. }else{
  37. $im = @imagecreate($width,$height);
  38. }
  39. $r = array(225,211,255,223);
  40. $g = array(225,236,237,215);
  41. $b = array(225,236,166,125);
  42. $key = rand(0,3);
  43. $backcolor = imagecolorallocate($im,$r[$key],$g[$key],$b[$key]);//背景色(随机)
  44. $bordercolor = imagecolorallocate($im, 0, 0, 0);//边框色
  45. $pointcolor = imagecolorallocate($im, 255, 170, 255);//点颜色
  46. @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backcolor);//背景位置
  47. @imagerectangle($im, 0, 0, $width-1, $height-1, $bordercolor); //边框位置
  48. $stringcolor = imagecolorallocate($im, 255,51,153);
  49. for($i=0;$i<=100;$i++){
  50. $pointx = rand(2,$width-2);
  51. $pointy = rand(2,$height-2);
  52. @imagesetpixel($im, $pointx, $pointy, $pointcolor);
  53. }
  54. @imagestring($im, 3, 5, 1, $randval, $stringcolor);
  55. $imagefun='image'.$type;
  56. $imagefun($im);
  57. @imagedestroy($im);
  58. $_session['safecode'] = $randval;
  59. //产生随机字符串
  60. function randstr($len=6,$format='all') {
  61. switch($format) {
  62. case 'all':
  63. $chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789www.111cn.net'; break;
  64. case 'char':
  65. $chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; break;
  66. case 'number':
  67. $chars='0123456789'; break;
  68. default :
  69. $chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789';
  70. break;
  71. }
  72. $string="";
  73. while(strlen($string)<$len)
  74. $string.=substr($chars,(mt_rand()%strlen($chars)),1);
  75. return $string;
  76. }
  77. //调用此页面,如果下面的式子成立,则生成验证码图片
  78. if($_get["action"]=="verifycode")
  79. {
  80. rand_create();
  81. }
  82. //验证码图片生成
  83. function rand_create()
  84. {
  85. //通知浏览器将要输出png图片
  86. header("content-type: image/png");
  87. //准备好随机数发生器种子
  88. srand((double)microtime()*1000000);
  89. //准备图片的相关参数
  90. $im = imagecreate(62,20);
  91. $black = imagecolorallocate($im, 0,0,0); //rgb黑色标识符
  92. $white = imagecolorallocate($im, 255,255,255); //rgb白色标识符
  93. $gray = imagecolorallocate($im, 200,200,200); //rgb灰色标识符
  94. //开始作图
  95. imagefill($im,0,0,$gray);
  96. while(($randval=rand()%100000)<10000);{
  97. $_session["login_check_num"] = $randval;
  98. //将四位整数验证码绘入图片
  99. imagestring($im, 5, 10, 3, $randval, $black);
  100. }
  101. //加入干扰象素
  102. for($i=0;$i<200;$i++){
  103. $randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  104. imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
  105. }
  106. //输出验证图片
  107. imagepng($im);
  108. //销毁图像标识符
  109. imagedestroy($im);
  110. }
  111. //检验验证码
  112. function rand_check()
  113. {
  114. if($_post["reg_rand"] == $_session["login_check_num"]){
  115. return true;
  116. } //开源代码phpfensi.com
  117. else{
  118. exit("验证码输入错误");
  119. }
  120. }

验证码通过gd生成png图片,并把$randval随机数字赋给$_session['login_check_num'],在通过用户输入的$_post进行比较,来判断是否正确,达到需要实现的功能,需要修改php.ini文件,使php支持gd库.