验证码通常用于:发送短信、用户登录、用户修改密码的场景。
1.验证码类封装-GD库验证
note:
- php版本匹配:version_compare()
- 检查一个函数是否存在:function_exists()
- 判断一个值是否是数值:is_numeric()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php /** * GDBasic.php * author: F.X * date: 2017 * description GD基础类 */ namespace Imooc\Lib; class GDBasic { protected static $_check =false; //检查服务器环境中gd库 public static function check() { //当静态变量不为false if(static::$_check) { return true; } //检查gd库是否加载 if(!function_exists("gd_info")) { throw new \Exception('GD is not exists'); } //检查gd库版本 $version = ''; $info = gd_info(); if(preg_match("/\\d+\\.\\d+(?:\\.\\d+)?/", $info["GD Version"], $matches)) { $version = $matches[0]; } //当gd库版本小于2.0.1 if(!version_compare($version,'2.0.1','>=')) { throw new \Exception("GD requires GD version '2.0.1' or greater, you have " . $version); } self::$_check = true; return self::$_check; } } |
2.输出验证码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
<?php /** * Captcha.php * author: F.X * date: 2017 * description 验证码类 */ namespace Imooc\Lib; require_once 'GDBasic.php'; class Captcha extends GDBasic { //图像宽度 protected $_width = 60; //图像高度 protected $_height = 25; //随机串 protected $_code = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjklmnpqrstuvwxyz'; //字体文件 protected $_font_file = './font/comicz.ttf'; //图像 protected $_im; //验证码 protected $_captcha; public function __construct($width = null, $height = null) { self::check(); $this->create($width, $height); } /** * 创建图像 * @param $width * @param $height */ public function create($width, $height) { $this->_width = is_numeric($width) ? $width : $this->_width; $this->_height = is_numeric($height) ? $height : $this->_height; //创建图像 $im = imagecreatetruecolor($this->_width, $this->_height); $back = imagecolorallocate($im, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)); //填充底色 imagefill($im, 0, 0, $back); $this->_im = $im; } /** * 混乱验证码 */ public function moll() { $back = imagecolorallocate($this->_im, 0, 0, 0); //在图像中随机生成50个点 for($i = 0; $i < 50; $i++) { imagesetpixel($this->_im, mt_rand(0, $this->_width), mt_rand(0, $this->_height), $back); } imageline($this->_im, mt_rand(0, $this->_width), mt_rand(0, $this->_height), mt_rand(0, $this->_width), mt_rand(0, $this->_height), $back); imageline($this->_im, mt_rand(0, $this->_width), mt_rand(0, $this->_height), mt_rand(0, $this->_width), mt_rand(0, $this->_height), $back); } /** * 生成验证码随机串 * @param int $length 验证码的个数 * @param int $fontSize 字符串的字体大小 * @return Captcha */ public function string($length = 4, $fontSize = 15) { $this->moll(); $code = $this->_code; $captcha = ''; for($i = 0; $i < $length; $i++) { $string = $code[mt_rand(0, strlen($code) - 1)]; $strColor = imagecolorallocate($this->_im, mt_rand(100, 150), mt_rand(100, 150), mt_rand(100, 150)); imagettftext($this->_im, $fontSize, mt_rand(-10, 10), mt_rand(3, 6) + $i * (($this->_width - 10) / $length), ($this->_height / 3) * 2, $strColor, $this->_font_file, $string); $captcha .= $string; } $this->_captcha = $captcha; return $this; } /** * 验证码存入session */ public function setSession() { if(!isset($_SESSION)) { session_start(); } $_SESSION['captcha_code'] = $this->_captcha; } /** * 逻辑运算符验证码 * @param int $fontSize 字体大小 * @return $this */ public function logic($fontSize = 12) { $this->moll(); $codeArray = array(1 => 1, 2, 3, 4, 5, 6, 7, 8, 9); $operatorArray = array('+' => '+', '-' => '-', 'x' => '*'); list($first, $second) = array_rand($codeArray, 2); $operator = array_rand($operatorArray); $captcha = 0; $string = ''; switch($operator) { case '+': $captcha = $first + $second; break; case '-': //当第一个数小于第二个数就交换,防止负数 if($first < $second) { list($first, $second) = array($second, $first); } $captcha = $first - $second; break; case 'x': $captcha = $first * $second; break; } //设置验证码类变量 $this->_captcha = $captcha; //要输出到图像中的字符串 $string = sprintf('%s%s%s=?', $first, $operator, $second); $strColor = imagecolorallocate($this->_im, mt_rand(100, 150), mt_rand(100, 150), mt_rand(100, 150)); imagettftext($this->_im, $fontSize, 0, 5, ($this->_height / 3) * 2, $strColor, $this->_font_file, $string); return $this; } /** * 输出验证码 */ public function show() { //生成session $this->setSession(); header('Content-Type:image/jpeg'); imagejpeg($this->_im); imagedestroy($this->_im); } } |
测试:index.php
1 2 3 4 5 6 |
<?php require_once './lib/Captcha.php'; $captcha = new Imooc\Lib\Captcha(80,30); $captcha -> string(6,4)->show(); |
看SESSION:demo.php
1 2 3 |
<?php session_start(); var_dump($_SESSION['captcha_code']); |