最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

实用PHP验证码类代码

时间:2014-09-09 11:07来源:未知 作者:最模板zuimoban 点击:
开发应用中验证码是少不了的,我们经常会碰以关于被机器注册,那么有了验证码可以有效的防止这类行为,下面我们来看看我提供的这款代码,实例代码如下: ?php session_start(); class authnum{

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

  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. // 获得随机色  
  60. function get_randcolor () {  
  61. $this->randcolor = imagecolorallocate($this->im,rand(0,100),rand(0,150),rand(0,200));  
  62. }  
  63. // 添加干扰点  
  64. function set_ext_pixel () {  
  65. if ($this->ext_pixel) {  
  66. for($i = 0; $i < 100; $i++){  
  67. $this->get_randcolor();  
  68. imagesetpixel($this->im, rand()%100, rand()%100, $this->randcolor);  
  69. }  
  70. }  
  71. }  
  72. // 添加干扰线  
  73. function set_ext_line () {  
  74. if ($this->ext_line) {  
  75. for($j = 0; $j < 2; $j++){  
  76. $rand_x = rand(2, $this->im_width);  
  77. $rand_y = rand(2, $this->im_height);  
  78. $rand_x2 = rand(2, $this->im_width);  
  79. $rand_y2 = rand(2, $this->im_height);  
  80. $this->get_randcolor();  
  81. imageline($this->im, $rand_x$rand_y$rand_x2$rand_y2$this->randcolor);  
  82. }  
  83. }  
  84. }  
  85. /**创建验证码图像:  
  86. * 建立画布(__construct函数)  
  87. * 设置画布背景($this->set_bgcolor();)  
  88. * 获取随机字符串($this->get_randnum ();)  
  89. * 文字写到图片上(imagestring函数)  
  90. * 添加干扰点/线($this->set_ext_line(); $this->set_ext_pixel();)  
  91. * 输出图片  
  92. **/  
  93. function create () {  
  94. $this->set_bgcolor();  
  95. $this->get_randnum ();  
  96. for($i = 0; $i < $this->len; $i++){  
  97. $font = rand(4,6);  
  98. $x = $i/$this->len * $this->im_width + rand(1, $this->len);  
  99. $this->get_y();  
  100. $this->get_randcolor();  
  101. imagestring($this->im, $font$x$this->y, substr($this->randnum, $i ,1), $this->randcolor);  
  102. }  
  103. $this->set_ext_line();  
  104. $this->set_ext_pixel();  
  105. header("content-type:image/png");  
  106. imagepng($this->im);  
  107. imagedestroy($this->im); //释放图像资源  
  108. }  
  109. }//end class  
  110. /**使用验证码类的方法:  
  111. * $an = new authnum(验证码长度,图片宽度,图片高度);  
  112. * 实例化时不带参数则默认是四位的60*25尺寸的常规验证码图片  
  113. * 表单页面检测验证码的方法,对比 $_session[an] 是否等于 $_post[验证码文本框id]  
  114. * 可选配置:  
  115. * 1.验证码类型:$an->ext_num_type=1; 值为1是小写类型,2是大写类型,3是数字类型  
  116. * 2.干扰点:$an->ext_pixel = false; 值为false表示不添加干扰点  
  117. * 3.干扰线:$an->ext_line = false; 值为false表示不添加干扰线  
  118. * 4.y轴随机:$an->ext_rand_y = false; 值为false表示不支持图片y轴随机  
  119. * 5.图片背景:改变 $red $green $blue 三个成员变量的值即可  
  120. **/  
  121. $an = new authnum();  
  122. //开源代码vcphp.com 
  123. $an->ext_num_type='';  
  124. $an->ext_pixel = true; //干扰点  
  125. $an->ext_line = false; //干扰线  
  126. $an->ext_rand_y= true; //y轴随机  
  127. $an->green = 238;  
  128. $an->create();  
  129. ?> 
  130.  
(责任编辑:最模板)
------分隔线----------------------------
栏目列表
推荐内容