PHP实现简单实用的验证码类

这篇文章主要介绍了PHP实现简单实用的验证码类,包含验证码常用的随机验证码、干扰线、图片生成与输出等相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了PHP实现简单实用的验证码类,分享给大家供大家参考,具体如下:

  1. <?php
  2. /**
  3. * @version 1.0
  4. * @author bolted snail
  5. * @date 2011-10-15
  6. * @PHP验证码类
  7. * 使用方法:
  8. * $image=new Captcha();
  9. * $image->config('宽度','高度','字符个数','验证码session索引');
  10. * $image->create();//这样就会向浏览器输出一张图片
  11. * //所有参数都可以省略,
  12. * 默认是:宽80 高20 字符数4 验证码session索引captcha_code
  13. * 第四个参数即把验证码存到$_SESSION['captcha_code']
  14. * 最简单使用示例:
  15. * $image=new Captcha();
  16. * $image->create();//这样就会向浏览器输出一张图片
  17. */
  18. class Captcha
  19. {
  20. private $width=80,$height=20,$codenum=4;
  21. public $checkcode; //产生的验证码
  22. private $checkimage; //验证码图片
  23. private $disturbColor = ''; //干扰像素
  24. private $session_flag='captcha_code';//存到session中的索引
  25. //尝试开始session
  26. function __construct(){
  27. @session_start();
  28. }
  29. /*
  30. * 参数:(宽度,高度,字符个数)
  31. */
  32. function config($width='80',$height='20',$codenum='4',$session_flag='captcha_code')
  33. {
  34. $this->width=$width;
  35. $this->height=$height;
  36. $this->codenum=$codenum;
  37. $this->session_flag=$session_flag;
  38. }
  39. function create()
  40. {
  41. //输出头
  42. $this->outFileHeader();
  43. //产生验证码
  44. $this->createCode();
  45. //产生图片
  46. $this->createImage();
  47. //设置干扰像素
  48. $this->setDisturbColor();
  49. //往图片上写验证码
  50. $this->writeCheckCodeToImage();
  51. imagepng($this->checkimage);
  52. imagedestroy($this->checkimage);
  53. $_SESSION[$this->session_flag]=$this->checkcode;
  54. }
  55. /*
  56. * @brief 输出头
  57. */
  58. private function outFileHeader()
  59. {
  60. header ("Content-type: image/png");
  61. }
  62. /**
  63. * 产生验证码
  64. */
  65. private function createCode()
  66. {
  67. $this->checkcode = strtoupper(substr(md5(rand()),0,$this->codenum));
  68. }
  69. /**
  70. * 产生验证码图片
  71. */
  72. private function createImage()
  73. {
  74. $this->checkimage = @imagecreate($this->width,$this->height);
  75. $back = imagecolorallocate($this->checkimage,255,255,255);
  76. $border = imagecolorallocate($this->checkimage,0,0,0);
  77. imagefilledrectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$back); // 白色底
  78. imagerectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$border); // 黑色边框
  79. }
  80. /**
  81. * 设置图片的干扰像素
  82. */
  83. private function setDisturbColor()
  84. {
  85. for ($i=0;$i<=200;$i++)
  86. {
  87. $this->disturbColor = imagecolorallocate($this->checkimage, rand(0,255), rand(0,255), rand(0,255));
  88. imagesetpixel($this->checkimage,rand(2,128),rand(2,38),$this->disturbColor);
  89. }
  90. }
  91. /**
  92. *
  93. * 在验证码图片上逐个画上验证码
  94. *
  95. */
  96. private function writeCheckCodeToImage()
  97. {
  98. for ($i=0;$i<$this->codenum;$i++)
  99. {
  100. $bg_color = imagecolorallocate ($this->checkimage, rand(0,255), rand(0,128), rand(0,255));
  101. $x = floor($this->width/$this->codenum)*$i;
  102. $y = rand(0,$this->height-15);
  103. imagechar ($this->checkimage, rand(5,8), $x+5, $y, $this->checkcode[$i], $bg_color);
  104. }
  105. }
  106. function __destruct()
  107. {
  108. unset($this->width,$this->height,$this->codenum,$this->session_flag);
  109. }
  110. }
  111. ?>

希望本文所述对大家的php程序设计有所帮助。