PHP验证码

PHP代码
  1. <?php    
  2. // --------------------------------------------------------------------------    
  3. // File name : seccode.php    
  4. // Description : Sort Info System    
  5. // Requirement : PHP4.2 (http://www.php.net)    
  6. //    
  7. // --------------------------------------------------------------------------    
  8.   
  9. session_start();    
  10. $_SESSION['seccode'] = "";    
  11. $width = 35;//图片长度    
  12. $height = 20;//图片高度    
  13. $len = 3;//验证码长度    
  14. $noise = true;//是否生成杂点    
  15. $noisenum = 60;//杂点数量    
  16. $border = true;//边框    
  17. $bordercolor = '#000000';    
  18. $bgcolor = "#d6e3ef";//背景色    
  19. $image = imagecreate($width,$height);    
  20. $back = getcolor($bgcolor);    
  21. imagefilledrectangle($image,0,0,$width,$height,$back);    
  22. $size = $width/$len;    
  23. if($size>$height$size=$height;    
  24. $left = ($width-$len*($size+$size/10))/$size;    
  25. $code = random($len);    
  26. $codecolor = imagecolorallocate($image, 0, 0, 0);    
  27. imagestring($image$size, 3, 3, $code$codecolor);    
  28. if($noise == true) setnoise();    
  29. $_SESSION['seccode'] = $code;    
  30. if($border) imageRectangle($image, 0, 0, $width-1, $height-1, getcolor($bordercolor));    
  31. header("Content-type: image/png");    
  32. imagePng($image);    
  33. imagedestroy($image);    
  34.   
  35. function getcolor($color)    
  36. {    
  37. global $image;    
  38. $color = eregi_replace ("^#","",$color);    
  39. $r = $color[0].$color[1];    
  40. $r = hexdec ($r);    
  41. $b = $color[2].$color[3];    
  42. $b = hexdec ($b);    
  43. $g = $color[4].$color[5];    
  44. $g = hexdec ($g);    
  45. $color = imagecolorallocate ($image$r$b$g);    
  46. return $color;    
  47. }    
  48. function random($length) {    
  49. $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';    
  50. $hash = '';    
  51. for($i = 0; $i < $length$i++) {    
  52. $hash .= $str[mt_rand(0,61)];    
  53. }    
  54. return $hash;    
  55. }    
  56. function setnoise()    
  57. {    
  58. global $image$width$height$back$noisenum;    
  59. for ($i=0; $i<$noisenum$i++){    
  60. $randColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));    
  61. imagesetpixel($image, rand(0, $width), rand(0, $height), $randColor);    
  62. }    
  63. }    
  64.   
  65. ?>    
  66.   
  67.   

上一篇: utf8转换gb2312   下一篇: PHP发Email

提交疑问

请先登录 QQ微博登录

回顶部