实用PHP验证码类代码

开发应用中验证码是少不了的,我们经常会碰以关于被机器注册,那么有了验证码可以有效的防止这类行为,下面我们来看看我提供的这款代码,实例代码如下:

  1. <?php
  2. session_start();
  3. class authnum {
  4. //图片对象、宽度、高度、验证码长度
  5. private $im;
  6. private $im_width;
  7. private $im_height;
  8. private $len;
  9. //随机字符串、y轴坐标值、随机颜色
  10. private $randnum;
  11. private $y;
  12. private $randcolor;
  13. //背景色的红绿蓝,默认是浅灰色
  14. public $red=238;
  15. public $green=238;
  16. public $blue=238;
  17. /**
  18. * 可选设置:验证码类型、干扰点、干扰线、y轴随机
  19. * 设为 false 表示不启用
  20. **/
  21. //默认是大小写数字混合型,1 2 3 分别表示 小写、大写、数字型
  22. public $ext_num_type='';
  23. public $ext_pixel = false; //干扰点
  24. public $ext_line = false; //干扰线
  25. public $ext_rand_y= true; //y轴随机
  26. function __construct ($len=4,$im_width='',$im_height=25) {
  27. // 验证码长度、图片宽度、高度是实例化类时必需的数据
  28. $this->len = $len; $im_width = $len * 15;
  29. $this->im_width = $im_width;
  30. $this->im_height= $im_height;
  31. $this->im = imagecreate($im_width,$im_height);
  32. }
  33. // 设置图片背景颜色,默认是浅灰色背景
  34. function set_bgcolor () {
  35. imagecolorallocate($this->im,$this->red,$this->green,$this->blue);
  36. }
  37. // 获得任意位数的随机码
  38. function get_randnum () {
  39. $an1 = 'abcdefghijklmnopqrstuvwxyz';
  40. $an2 = 'abcdefghijklmnopqrstuvwxyz';
  41. $an3 = '0123456789';
  42. if ($this->ext_num_type == '') $str = $an1.$an2.$an3;
  43. if ($this->ext_num_type == 1) $str = $an1;
  44. if ($this->ext_num_type == 2) $str = $an2;
  45. if ($this->ext_num_type == 3) $str = $an3;
  46. for ($i = 0; $i < $this->len; $i++) {
  47. $start = rand(1,strlen($str) - 1);
  48. $randnum .= substr($str,$start,1);
  49. }
  50. $this->randnum = $randnum;
  51. $_session[an] = $this->randnum;
  52. }
  53. // 获得验证码图片y轴
  54. function get_y () {
  55. if ($this->ext_rand_y) $this->y = rand(5, $this->im_height/5);
  56. else $this->y = $this->im_height / 4 ;
  57. }
  58. // 获得随机色
  59. function get_randcolor () {
  60. $this->randcolor = imagecolorallocate($this->im,rand(0,100),rand(0,150),rand(0,200));
  61. }
  62. // 添加干扰点
  63. function set_ext_pixel () {
  64. if ($this->ext_pixel) {
  65. for($i = 0; $i < 100; $i++){
  66. $this->get_randcolor();
  67. imagesetpixel($this->im, rand()%100, rand()%100, $this->randcolor);
  68. }
  69. }
  70. }
  71. // 添加干扰线
  72. function set_ext_line () {
  73. if ($this->ext_line) {
  74. for($j = 0; $j < 2; $j++){
  75. $rand_x = rand(2, $this->im_width);
  76. $rand_y = rand(2, $this->im_height);
  77. $rand_x2 = rand(2, $this->im_width);
  78. $rand_y2 = rand(2, $this->im_height);
  79. $this->get_randcolor();
  80. imageline($this->im, $rand_x, $rand_y, $rand_x2, $rand_y2, $this->randcolor);
  81. }
  82. }
  83. }
  84. /**创建验证码图像:
  85. * 建立画布(__construct函数)
  86. * 设置画布背景($this->set_bgcolor();)
  87. * 获取随机字符串($this->get_randnum ();)
  88. * 文字写到图片上(imagestring函数)
  89. * 添加干扰点/线($this->set_ext_line(); $this->set_ext_pixel();)
  90. * 输出图片
  91. **/
  92. function create () {
  93. $this->set_bgcolor();
  94. $this->get_randnum ();
  95. for($i = 0; $i < $this->len; $i++){
  96. $font = rand(4,6);
  97. $x = $i/$this->len * $this->im_width + rand(1, $this->len);
  98. $this->get_y();
  99. $this->get_randcolor();
  100. imagestring($this->im, $font, $x, $this->y, substr($this->randnum, $i ,1), $this->randcolor);
  101. }
  102. $this->set_ext_line();
  103. $this->set_ext_pixel();
  104. header("content-type:image/png");
  105. imagepng($this->im);
  106. imagedestroy($this->im); //释放图像资源
  107. }
  108. }//end class
  109. /**使用验证码类的方法:
  110. * $an = new authnum(验证码长度,图片宽度,图片高度);
  111. * 实例化时不带参数则默认是四位的60*25尺寸的常规验证码图片
  112. * 表单页面检测验证码的方法,对比 $_session[an] 是否等于 $_post[验证码文本框id]
  113. * 可选配置:
  114. * 1.验证码类型:$an->ext_num_type=1; 值为1是小写类型,2是大写类型,3是数字类型
  115. * 2.干扰点:$an->ext_pixel = false; 值为false表示不添加干扰点
  116. * 3.干扰线:$an->ext_line = false; 值为false表示不添加干扰线
  117. * 4.y轴随机:$an->ext_rand_y = false; 值为false表示不支持图片y轴随机
  118. * 5.图片背景:改变 $red $green $blue 三个成员变量的值即可
  119. **/
  120. $an = new authnum();
  121. //开源代码phpfensi.com
  122. $an->ext_num_type='';
  123. $an->ext_pixel = true; //干扰点
  124. $an->ext_line = false; //干扰线
  125. $an->ext_rand_y= true; //y轴随机
  126. $an->green = 238;
  127. $an->create();
  128. ?>