diff --git a/README.md b/README.md index 5158151..332d696 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ thinkphp5.2 验证码类库 ## 安装 > composer require topthink/think-captcha +## 在您的项目路由文件里添加路由支持 +> Route::get('/captcha/get_validate_code/[:id]', "\\think\\captcha\\CaptchaController@index"); ##使用 @@ -30,4 +32,4 @@ $this->validate($data,[ if(!captcha_check($captcha)){ //验证失败 }; -~~~ \ No newline at end of file +~~~ diff --git a/src/Captcha.php b/src/Captcha.php index 29f9dad..54636a5 100644 --- a/src/Captcha.php +++ b/src/Captcha.php @@ -8,15 +8,13 @@ // +---------------------------------------------------------------------- // | Author: yunwuxin <448901948@qq.com> // +---------------------------------------------------------------------- - namespace think\captcha; - use think\facade\Session; class Captcha { protected $config = [ - 'seKey' => 'ThinkPHP.CN', + 'seKey' => 'Wmnrj.Com', // 验证码加密密钥 'codeSet' => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY', // 验证码字符集合 @@ -106,20 +104,22 @@ public function __isset($name) */ public function check($code, $id = '') { + $key = $this->authcode($this->seKey) . $id; + // 验证码不能为空 - $secode = Session::get($key, ''); + $secode = Session::get($key); if (empty($code) || empty($secode)) { return false; } // session 过期 if (time() - $secode['verify_time'] > $this->expire) { - Session::delete($key, ''); + Session::delete($key); return false; } if ($this->authcode(strtoupper($code)) == $secode['verify_code']) { - $this->reset && Session::delete($key, ''); + $this->reset && Session::delete($key); return true; } @@ -158,7 +158,9 @@ public function entry($id = '') } } $dir->close(); - $this->fontttf = $ttfs[array_rand($ttfs)]; + $this->fontttf = $ttfs[1]; + }else{ + $this->fontttf = $this->fontttf.'.ttf'; } $this->fontttf = $ttfPath . $this->fontttf; @@ -187,8 +189,8 @@ public function entry($id = '') } else { for ($i = 0; $i < $this->length; $i++) { $code[$i] = $this->codeSet[mt_rand(0, strlen($this->codeSet) - 1)]; - $codeNX += mt_rand($this->fontSize * 1.2, $this->fontSize * 1.6); - imagettftext($this->im, $this->fontSize, mt_rand(-40, 40), $codeNX, $this->fontSize * 1.6, $this->color, $this->fontttf, $code[$i]); + $codeNX += $this->fontSize * 1.3; + imagettftext($this->im, $this->fontSize, 0, $codeNX, $this->fontSize * 1.4, $this->color, $this->fontttf, $code[$i]); } } @@ -196,9 +198,9 @@ public function entry($id = '') $key = $this->authcode($this->seKey); $code = $this->authcode(strtoupper(implode('', $code))); $secode = []; - $secode['verify_code'] = $code; // 把校验码保存到session - $secode['verify_time'] = time(); // 验证码创建时间 - Session::set($key . $id, $secode, ''); + $secode['verify_code'] = $code; + $secode['verify_time'] = time(); + Session::set($key . $id, $secode); ob_start(); // 输出图像 diff --git a/src/helper.php b/src/helper.php index 7dd3cfb..6582024 100644 --- a/src/helper.php +++ b/src/helper.php @@ -9,13 +9,11 @@ // | Author: yunwuxin <448901948@qq.com> // +---------------------------------------------------------------------- -Route::get('captcha/[:id]', "\\think\\captcha\\CaptchaController@index"); - -Validate::extend('captcha', function ($value, $id = '') { +think\facade\Validate::extend('captcha', function ($value, $id = '') { return captcha_check($value, $id); }); -Validate::setTypeMsg('captcha', ':attribute错误!'); +think\facade\Validate::setTypeMsg('captcha', ':attribute错误!'); /** * @param string $id @@ -34,7 +32,7 @@ function captcha($id = '', $config = []) */ function captcha_src($id = '') { - return Url::build('/captcha' . ($id ? "/{$id}" : '')); + return think\facade\Url::build('/captcha/get_validate_code/' . ($id ? "/{$id}" : ''),[],'','api'); } /** @@ -46,6 +44,16 @@ function captcha_img($id = '') return 'captcha'; } +/** + * @param string $id + * @param string $element 验证码HTML元素ID + * @return string + */ +function captcha_img_with_replacement($id = '', $element = 'alimx-captcha') +{ + return 'captcha'; +} + /** * @param $value * @param string $id @@ -54,6 +62,6 @@ function captcha_img($id = '') */ function captcha_check($value, $id = '') { - $captcha = new \think\captcha\Captcha((array) Config::pull('captcha')); + $captcha = new \think\captcha\Captcha((array) \think\facade\Config::pull('captcha')); return $captcha->check($value, $id); }