Verify.class.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace My;
  3. /**
  4. * 验证码驱动
  5. * @author Devil
  6. * @blog http://gong.gg/
  7. * @version 0.0.1
  8. * @datetime 2016-12-01T21:51:08+0800
  9. */
  10. class Verify
  11. {
  12. private $img;
  13. private $rand_string;
  14. private $width;
  15. private $height;
  16. private $length;
  17. private $use_point_back;
  18. private $use_line_back;
  19. private $use_color_back;
  20. private $key_verify;
  21. private $expire_time;
  22. /**
  23. * [__construct 构造方法]
  24. * @author Devil
  25. * @blog http://gong.gg/
  26. * @version 0.0.1
  27. * @datetime 2017-03-05T16:42:49+0800
  28. * @param [int] $param['width'] [宽度(默认65)]
  29. * @param [int] $param['height'] [高度(默认30)]
  30. * @param [int] $param['length'] [验证码位数(默认6)]
  31. * @param [boolean] $param['use_point_back'] [是否添加干扰点(默认 true)]
  32. * @param [boolean] $param['use_line_back'] [是否添加干扰线(默认 true)]
  33. * @param [boolean] $param['use_color_back'] [是否使用彩色背景(默认 true)]
  34. * @param [string] $param['key_prefix'] [验证码种存储前缀key(默认 空)]
  35. * @param [int] $param['expire_time'] [到期时间(默认30)单位(秒)]
  36. */
  37. public function __construct($param = array())
  38. {
  39. // 参数处理
  40. $this->width = isset($param['width']) ? intval($param['width']) : 65;
  41. $this->height = isset($param['height']) ? intval($param['height']) : 30;
  42. $this->length = isset($param['length']) ? intval($param['length']) : 6;
  43. $this->use_point_back = isset($param['use_point_back']) ? $param['use_point_back'] : true;
  44. $this->use_line_back = isset($param['use_line_back']) ? $param['use_line_back'] : true;
  45. $this->use_color_back = isset($param['use_color_back']) ? $param['use_color_back'] : true;
  46. $this->key_verify = isset($param['key_prefix']) ? trim($param['key_prefix']).'_verify_code' : '_verify_code';
  47. $this->expire_time = isset($param['expire_time']) ? intval($param['expire_time']) : 30;
  48. }
  49. /**
  50. * [Entry 验证码生成]
  51. * @author Devil
  52. * @blog http://gong.gg/
  53. * @version 0.0.1
  54. * @datetime 2017-03-05T16:55:19+0800
  55. */
  56. public function Entry()
  57. {
  58. // 验证码生成
  59. $this->rand_string = $this->GetRandString();
  60. // 创建一个画布(真色彩)
  61. $this->img = imagecreatetruecolor($this->width, $this->height);
  62. // 画背景
  63. if($this->use_color_back == true)
  64. {
  65. $back_color = imagecolorallocate($this->img, rand(200,255), rand(200,255), rand(200,255));
  66. } else {
  67. $back_color = imagecolorallocate($this->img, 255, 255, 255);
  68. }
  69. imagefilledrectangle($this->img, 0, 0, $this->width, $this->height, $back_color);
  70. // 加入干扰,画出多条线
  71. if($this->use_line_back == true)
  72. {
  73. $this->InterferenceLine();
  74. }
  75. // 加入干扰,画出点
  76. if($this->use_point_back == true)
  77. {
  78. $this->InterferencePoint();
  79. }
  80. // 将生成好的字符串写入图像
  81. $each_width = intval($this->width/$this->length);
  82. $first = 40/100*$each_width;
  83. foreach(str_split($this->rand_string) as $k=>$v)
  84. {
  85. $fgcolor = imagecolorallocate($this->img, rand(0,200), rand(0,255), rand(0,255));
  86. $temp_height = 95/100*$this->height;
  87. if($this->height-$temp_height < 15)
  88. {
  89. $temp_height = $this->height-15;
  90. }
  91. imagestring($this->img, rand(3,5), $k*$each_width+$first, rand(5/100*$this->height,$temp_height), strtoupper($v), $fgcolor);
  92. }
  93. // 种session
  94. $this->KindofSession();
  95. // 输出图像
  96. header('Content-Type: image/gif');
  97. imagegif($this->img);
  98. // 销毁图像
  99. imagedestroy($this->img);
  100. }
  101. /**
  102. * [CheckExpire 验证码是否过期]
  103. * @author Devil
  104. * @blog http://gong.gg/
  105. * @version 0.0.1
  106. * @datetime 2017-03-05T19:02:26+0800
  107. * @return [boolean] [有效true, 无效false]
  108. */
  109. public function CheckExpire()
  110. {
  111. if(isset($_SESSION[$this->key_verify]))
  112. {
  113. $data = $_SESSION[$this->key_verify];
  114. return (time() <= $data['time']+$this->expire_time);
  115. }
  116. return false;
  117. }
  118. /**
  119. * [CheckCorrect 验证码是否正确]
  120. * @author Devil
  121. * @blog http://gong.gg/
  122. * @version 0.0.1
  123. * @datetime 2017-03-05T16:55:00+0800
  124. * @param [string] $verify [验证码(默认从post读取)]
  125. * @return [booolean] [正确true, 错误false]
  126. */
  127. public function CheckCorrect($verify = '')
  128. {
  129. if(isset($_SESSION[$this->key_verify]['verify']))
  130. {
  131. if(empty($verify) && isset($_POST['verify']))
  132. {
  133. $verify = trim($_POST['verify']);
  134. }
  135. return ($_SESSION[$this->key_verify]['verify'] == strtolower($verify));
  136. }
  137. return false;
  138. }
  139. /**
  140. * [Remove 验证码清除]
  141. * @author Devil
  142. * @blog http://gong.gg/
  143. * @version 0.0.1
  144. * @datetime 2017-03-08T10:18:20+0800
  145. * @return [other] [无返回值]
  146. */
  147. public function Remove()
  148. {
  149. if(isset($_SESSION[$this->key_verify]))
  150. {
  151. unset($_SESSION[$this->key_verify]);
  152. }
  153. }
  154. /**
  155. * [KindofSession 种验证码session]
  156. * @author Devil
  157. * @blog http://gong.gg/
  158. * @version 0.0.1
  159. * @datetime 2017-03-05T18:52:45+0800
  160. */
  161. private function KindofSession()
  162. {
  163. $_SESSION[$this->key_verify] = array(
  164. 'verify' => $this->rand_string,
  165. 'time' => time(),
  166. );
  167. }
  168. /**
  169. * [InterferencePoint 加入干扰,画点]
  170. * @author Devil
  171. * @blog http://gong.gg/
  172. * @version 0.0.1
  173. * @datetime 2017-03-05T16:55:34+0800
  174. */
  175. private function InterferencePoint()
  176. {
  177. for($i=0; $i<200; $i++)
  178. {
  179. //产生随机的颜色
  180. $bgcolor = imagecolorallocate($this->img, rand(0,255), rand(0,255), rand(0,255));
  181. imagesetpixel($this->img, rand()%$this->width, rand()%$this->height, $bgcolor);
  182. }
  183. }
  184. /**
  185. * [InterferenceLine 加入干扰,画出多条线]
  186. * @author Devil
  187. * @blog http://gong.gg/
  188. * @version 0.0.1
  189. * @datetime 2017-03-05T16:55:46+0800
  190. */
  191. private function InterferenceLine()
  192. {
  193. for($i=0; $i<5; $i++)
  194. {
  195. //产生随机的颜色
  196. $bgcolor = imagecolorallocate($this->img, rand(0,255), rand(0,255), rand(0,255));
  197. imageline($this->img, rand(10,90), 0, rand(0,$this->width*2), $this->height, $bgcolor);
  198. }
  199. }
  200. /**
  201. * [GetRandString 生成随机数值]
  202. * @author Devil
  203. * @blog http://gong.gg/
  204. * @version 0.0.1
  205. * @datetime 2017-03-05T16:55:54+0800
  206. */
  207. private function GetRandString()
  208. {
  209. $origstr = '3456789abxdefghijkmnprstuvwxy';
  210. $string = '';
  211. $len = strlen($origstr);
  212. for($i=0; $i<$this->length; $i++)
  213. {
  214. $index = mt_rand(0, $len-1);
  215. $char = $origstr[$index];
  216. $string .= $char;
  217. }
  218. return $string;
  219. }
  220. }
  221. ?>