Secoder.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace Qii\Library;
  3. /**
  4. * 安全验证码
  5. *
  6. * 安全的验证码要:验证码文字扭曲、旋转,使用不同字体,添加干扰码
  7. *
  8. * @author 流水孟春 <cmpan(at)qq.com>
  9. * @link http://labs.yulans.cn/YL_Security_Secoder
  10. * @link http://wiki.yulans.cn/docs/yl/security/secoder
  11. */
  12. class Secoder {
  13. /**
  14. * 验证码的session的下标
  15. *
  16. * @var string
  17. */
  18. public static $seKey = 'sid.sekey.ylans.cn';
  19. public static $expire = 3000; // 验证码过期时间(s)
  20. /**
  21. * 验证码中使用的字符,01IO容易混淆,建议不用
  22. *
  23. * @var string
  24. */
  25. public static $codeSet = '346789ABCDEFGHJKLMNPQRTUVWXY';
  26. public static $fontSize = 25; // 验证码字体大小(px)
  27. public static $useCurve = true; // 是否画混淆曲线
  28. public static $useNoise = true; // 是否添加杂点
  29. public static $imageH = 0; // 验证码图片宽
  30. public static $imageL = 0; // 验证码图片长
  31. public static $length = 4; // 验证码位数
  32. public static $bg = array(243, 251, 254); // 背景
  33. protected static $_image = null; // 验证码图片实例
  34. protected static $_color = null; // 验证码字体颜色
  35. /**
  36. * 输出验证码并把验证码的值保存的session中
  37. * 验证码保存到session的格式为: $_SESSION[self::$seKey] = array('code' => '验证码值', 'time' => '验证码创建时间');
  38. */
  39. public static function entry() {
  40. // 图片宽(px)
  41. self::$imageL || self::$imageL = self::$length * self::$fontSize * 1.5 + self::$fontSize*1.5;
  42. // 图片高(px)
  43. self::$imageH || self::$imageH = self::$fontSize * 2;
  44. // 建立一幅 self::$imageL x self::$imageH 的图像
  45. self::$_image = imagecreate(self::$imageL, self::$imageH);
  46. // 设置背景
  47. imagecolorallocate(self::$_image, self::$bg[0], self::$bg[1], self::$bg[2]);
  48. // 验证码字体随机颜色
  49. self::$_color = imagecolorallocate(self::$_image, mt_rand(1,120), mt_rand(1,120), mt_rand(1,120));
  50. // 验证码使用随机字体
  51. $ttf = dirname(__FILE__) . '/ttfs/' . mt_rand(1, 20) . '.ttf';
  52. if (self::$useNoise) {
  53. // 绘杂点
  54. self::_writeNoise();
  55. }
  56. if (self::$useCurve) {
  57. // 绘干扰线
  58. self::_writeCurve();
  59. }
  60. // 绘验证码
  61. $code = array(); // 验证码
  62. $codeNX = 0; // 验证码第N个字符的左边距
  63. for ($i = 0; $i<self::$length; $i++) {
  64. $code[$i] = self::$codeSet[mt_rand(0, 27)];
  65. $codeNX += mt_rand(self::$fontSize*1.2, self::$fontSize*1.6);
  66. // 写一个验证码字符
  67. imagettftext(self::$_image, self::$fontSize, mt_rand(-40, 70), $codeNX, self::$fontSize*1.5, self::$_color, $ttf, $code[$i]);
  68. }
  69. // 保存验证码
  70. isset($_SESSION) || session_start();
  71. $_SESSION[self::$seKey]['code'] = join('', $code); // 把校验码保存到session
  72. $_SESSION[self::$seKey]['time'] = time(); // 验证码创建时间
  73. header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate');
  74. header('Cache-Control: post-check=0, pre-check=0', false);
  75. header('Pragma: no-cache');
  76. header("content-type: image/png");
  77. // 输出图像
  78. imagepng(self::$_image);
  79. imagedestroy(self::$_image);
  80. }
  81. /**
  82. * 画一条由两条连在一起构成的随机正弦函数曲线作干扰线(你可以改成更帅的曲线函数)
  83. *
  84. * 高中的数学公式咋都忘了涅,写出来
  85. * 正弦型函数解析式:y=Asin(ωx+φ)+b
  86. * 各常数值对函数图像的影响:
  87. * A:决定峰值(即纵向拉伸压缩的倍数)
  88. * b:表示波形在Y轴的位置关系或纵向移动距离(上加下减)
  89. * φ:决定波形与X轴位置关系或横向移动距离(左加右减)
  90. * ω:决定周期(最小正周期T=2π/∣ω∣)
  91. *
  92. */
  93. protected static function _writeCurve() {
  94. $A = mt_rand(1, self::$imageH/2); // 振幅
  95. $b = mt_rand(-self::$imageH/4, self::$imageH/4); // Y轴方向偏移量
  96. $f = mt_rand(-self::$imageH/4, self::$imageH/4); // X轴方向偏移量
  97. $T = mt_rand(self::$imageH*1.5, self::$imageL*2); // 周期
  98. $w = (2* M_PI)/$T;
  99. $px1 = 0; // 曲线横坐标起始位置
  100. $px2 = mt_rand(self::$imageL/2, self::$imageL * 0.667); // 曲线横坐标结束位置
  101. for ($px=$px1; $px<=$px2; $px=$px+ 0.9) {
  102. if ($w!=0) {
  103. $py = $A * sin($w*$px + $f)+ $b + self::$imageH/2; // y = Asin(ωx+φ) + b
  104. $i = (int) ((self::$fontSize - 6)/4);
  105. while ($i > 0) {
  106. imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // 这里画像素点比imagettftext和imagestring性能要好很多
  107. $i--;
  108. }
  109. }
  110. }
  111. $A = mt_rand(1, self::$imageH/2); // 振幅
  112. $f = mt_rand(-self::$imageH/4, self::$imageH/4); // X轴方向偏移量
  113. $T = mt_rand(self::$imageH*1.5, self::$imageL*2); // 周期
  114. $w = (2* M_PI)/$T;
  115. $b = $py - $A * sin($w*$px + $f) - self::$imageH/2;
  116. $px1 = $px2;
  117. $px2 = self::$imageL;
  118. for ($px=$px1; $px<=$px2; $px=$px+ 0.9) {
  119. if ($w!=0) {
  120. $py = $A * sin($w*$px + $f)+ $b + self::$imageH/2; // y = Asin(ωx+φ) + b
  121. $i = (int) ((self::$fontSize - 8)/4);
  122. while ($i > 0) {
  123. imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出(不用这while循环)性能要好很多
  124. $i--;
  125. }
  126. }
  127. }
  128. }
  129. /**
  130. * 画杂点
  131. * 往图片上写不同颜色的字母或数字
  132. */
  133. protected static function _writeNoise() {
  134. for($i = 0; $i < 10; $i++){
  135. //杂点颜色
  136. $noiseColor = imagecolorallocate(
  137. self::$_image,
  138. mt_rand(150,225),
  139. mt_rand(150,225),
  140. mt_rand(150,225)
  141. );
  142. for($j = 0; $j < 5; $j++) {
  143. // 绘杂点
  144. imagestring(
  145. self::$_image,
  146. 5,
  147. mt_rand(-10, self::$imageL),
  148. mt_rand(-10, self::$imageH),
  149. self::$codeSet[mt_rand(0, 27)], // 杂点文本为随机的字母或数字
  150. $noiseColor
  151. );
  152. }
  153. }
  154. }
  155. /**
  156. * 验证验证码是否正确
  157. *
  158. * @param string $code 用户验证码
  159. * @param bool 用户验证码是否正确
  160. */
  161. public static function check($code) {
  162. isset($_SESSION) || session_start();
  163. // 验证码不能为空
  164. if(empty($code) || empty($_SESSION[self::$seKey])) {
  165. return false;
  166. }
  167. // session 过期
  168. if(time() - $_SESSION[self::$seKey]['time'] > self::$expire) {
  169. unset($_SESSION[self::$seKey]);
  170. return false;
  171. }
  172. if($code == $_SESSION[self::$seKey]['code']) {
  173. return true;
  174. }
  175. return false;
  176. }
  177. }
  178. // useage
  179. /*
  180. YL_Security_Secoder::$useNoise = false; // 要更安全的话改成true
  181. YL_Security_Secoder::$useCurve = true;
  182. YL_Security_Secoder::entry();
  183. */
  184. /*
  185. // 验证验证码
  186. if (!YL_Security_Secoder::check(@$_POST['secode'])) {
  187. print 'error secode';
  188. }
  189. */