Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ thinkphp5.2 验证码类库
## 安装
> composer require topthink/think-captcha

## 在您的项目路由文件里添加路由支持
> Route::get('/captcha/get_validate_code/[:id]', "\\think\\captcha\\CaptchaController@index");

##使用

Expand All @@ -30,4 +32,4 @@ $this->validate($data,[
if(!captcha_check($captcha)){
//验证失败
};
~~~
~~~
26 changes: 14 additions & 12 deletions src/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
// 验证码字符集合
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -187,18 +189,18 @@ 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]);
}
}

// 保存验证码
$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();
// 输出图像
Expand Down
20 changes: 14 additions & 6 deletions src/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
}

/**
Expand All @@ -46,6 +44,16 @@ function captcha_img($id = '')
return '<img src="' . captcha_src($id) . '" alt="captcha" />';
}

/**
* @param string $id
* @param string $element 验证码HTML元素ID
* @return string
*/
function captcha_img_with_replacement($id = '', $element = 'alimx-captcha')
{
return '<img src="' . captcha_src($id) . '" alt="captcha" class="' . $element . '" onclick="this.src=\''.captcha_src($id).'?\' + Math.random();" />';
}

/**
* @param $value
* @param string $id
Expand All @@ -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);
}