From 5736810658a350f7da1261ab35e6b5baa07240c2 Mon Sep 17 00:00:00 2001 From: rise Date: Mon, 7 Jan 2019 14:14:46 +0800 Subject: [PATCH 1/6] use cache --- README.md | 2 +- composer.json | 8 ++++++-- src/Captcha.php | 32 ++++++++++++++++++++++++++------ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4a6c25c..7ad202d 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ thinkphp5.1 验证码类库 ## 安装 -> composer require topthink/think-captcha +> composer require rise-worlds/think-cache-captcha ##使用 diff --git a/composer.json b/composer.json index d5350d4..a35e927 100644 --- a/composer.json +++ b/composer.json @@ -1,10 +1,14 @@ { - "name": "topthink/think-captcha", - "description": "captcha package for thinkphp5", + "name": "rise-worlds/think-cache-captcha", + "description": "captcha package for thinkphp5, use cache", "authors": [ { "name": "yunwuxin", "email": "448901948@qq.com" + }, + { + "name": "rise", + "email": "rise.worlds@outlook.com" } ], "license": "Apache-2.0", diff --git a/src/Captcha.php b/src/Captcha.php index 29f9dad..a1b46d1 100644 --- a/src/Captcha.php +++ b/src/Captcha.php @@ -12,6 +12,8 @@ namespace think\captcha; use think\facade\Session; +use think\facade\Cache; +use think\facade\App; class Captcha { @@ -38,7 +40,7 @@ class Captcha // 验证码图片高度 'imageW' => 0, // 验证码图片宽度 - 'length' => 5, + 'length' => 4, // 验证码位数 'fontttf' => '', // 验证码字体,不设置随机获取 @@ -108,18 +110,21 @@ public function check($code, $id = '') { $key = $this->authcode($this->seKey) . $id; // 验证码不能为空 - $secode = Session::get($key, ''); + // $secode = Session::get($key, ''); + $secode = Cache::get($key); if (empty($code) || empty($secode)) { return false; } // session 过期 if (time() - $secode['verify_time'] > $this->expire) { - Session::delete($key, ''); + // Session::delete($key, ''); + Cache::rm($key); return false; } if ($this->authcode(strtoupper($code)) == $secode['verify_code']) { - $this->reset && Session::delete($key, ''); + // $this->reset && Session::delete($key, ''); + $this->reset && Cache::rm($key); return true; } @@ -198,7 +203,20 @@ public function entry($id = '') $secode = []; $secode['verify_code'] = $code; // 把校验码保存到session $secode['verify_time'] = time(); // 验证码创建时间 - Session::set($key . $id, $secode, ''); + // Session::set($key . $id, $secode, ''); + //保存验证码到缓存 + $options = [ + // 缓存类型为File + 'type' => 'File', + // 缓存有效期为永久有效 + 'expire'=> 0, + //缓存前缀 + 'prefix'=> 'think', + // 指定缓存目录 + 'path' => App::getAppPath().'runtime/cache/', + ]; + Cache::connect($options); + Cache::set($key . $id, $secode, $this->expire); ob_start(); // 输出图像 @@ -206,7 +224,9 @@ public function entry($id = '') $content = ob_get_clean(); imagedestroy($this->im); - return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png'); + // return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png'); + $content = 'data:image/png;base64,'.base64_encode($content); + return response($content, 200, ['Content-Length' => strlen($content)])->contentType('text/plain'); } /** From cd604c9cdf628ad0080c85e6c98be795989d088d Mon Sep 17 00:00:00 2001 From: rise Date: Mon, 7 Jan 2019 15:00:32 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E8=BF=94=E5=9B=9Ebase64=E7=BC=96=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Captcha.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Captcha.php b/src/Captcha.php index a1b46d1..c008787 100644 --- a/src/Captcha.php +++ b/src/Captcha.php @@ -48,6 +48,8 @@ class Captcha // 背景颜色 'reset' => true, // 验证成功后是否重置 + 'base64' => true, + // 返回base64编码图片 ]; private $im = null; // 验证码图片实例 @@ -224,9 +226,12 @@ public function entry($id = '') $content = ob_get_clean(); imagedestroy($this->im); - // return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png'); - $content = 'data:image/png;base64,'.base64_encode($content); - return response($content, 200, ['Content-Length' => strlen($content)])->contentType('text/plain'); + if ($this->base64) { + $content = 'data:image/png;base64,'.base64_encode($content); + return response($content, 200, ['Content-Length' => strlen($content)])->contentType('text/plain'); + } else { + return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png'); + } } /** From af014cd42e7748b2d6bf523b581c36e52b5a8da3 Mon Sep 17 00:00:00 2001 From: rise Date: Mon, 7 Jan 2019 15:48:11 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E9=BB=98=E8=AE=A4=E4=B8=8D=E4=BD=BF?= =?UTF-8?q?=E7=94=A8base64=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Captcha.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Captcha.php b/src/Captcha.php index c008787..e59f671 100644 --- a/src/Captcha.php +++ b/src/Captcha.php @@ -48,7 +48,7 @@ class Captcha // 背景颜色 'reset' => true, // 验证成功后是否重置 - 'base64' => true, + 'base64' => false, // 返回base64编码图片 ]; From b539c17c6328fba808cb722a8d13fecf0c9d7cf9 Mon Sep 17 00:00:00 2001 From: rise Date: Sat, 8 May 2021 11:54:11 +0800 Subject: [PATCH 4/6] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7ad202d..973f3b2 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ thinkphp5.1 验证码类库 ## 安装 -> composer require rise-worlds/think-cache-captcha +> composer require topthink/think-captcha ##使用 @@ -30,4 +30,4 @@ $this->validate($data,[ if(!captcha_check($captcha)){ //验证失败 }; -~~~ \ No newline at end of file +~~~ From f4b4d9a0222c3127a6a7183186bf3cdf9d18697f Mon Sep 17 00:00:00 2001 From: rise Date: Sat, 8 May 2021 11:55:02 +0800 Subject: [PATCH 5/6] Update composer.json --- composer.json | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index a35e927..d5350d4 100644 --- a/composer.json +++ b/composer.json @@ -1,14 +1,10 @@ { - "name": "rise-worlds/think-cache-captcha", - "description": "captcha package for thinkphp5, use cache", + "name": "topthink/think-captcha", + "description": "captcha package for thinkphp5", "authors": [ { "name": "yunwuxin", "email": "448901948@qq.com" - }, - { - "name": "rise", - "email": "rise.worlds@outlook.com" } ], "license": "Apache-2.0", From e73ba44fd24fb1633cc006db24efbde4a460467f Mon Sep 17 00:00:00 2001 From: rise Date: Sat, 8 May 2021 11:58:16 +0800 Subject: [PATCH 6/6] Update Captcha.php --- src/Captcha.php | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/src/Captcha.php b/src/Captcha.php index e59f671..c9ae867 100644 --- a/src/Captcha.php +++ b/src/Captcha.php @@ -12,8 +12,6 @@ namespace think\captcha; use think\facade\Session; -use think\facade\Cache; -use think\facade\App; class Captcha { @@ -40,7 +38,7 @@ class Captcha // 验证码图片高度 'imageW' => 0, // 验证码图片宽度 - 'length' => 4, + 'length' => 5, // 验证码位数 'fontttf' => '', // 验证码字体,不设置随机获取 @@ -112,21 +110,18 @@ public function check($code, $id = '') { $key = $this->authcode($this->seKey) . $id; // 验证码不能为空 - // $secode = Session::get($key, ''); - $secode = Cache::get($key); + $secode = Session::get($key, ''); if (empty($code) || empty($secode)) { return false; } // session 过期 if (time() - $secode['verify_time'] > $this->expire) { - // Session::delete($key, ''); - Cache::rm($key); + Session::delete($key, ''); return false; } if ($this->authcode(strtoupper($code)) == $secode['verify_code']) { - // $this->reset && Session::delete($key, ''); - $this->reset && Cache::rm($key); + $this->reset && Session::delete($key, ''); return true; } @@ -205,20 +200,7 @@ public function entry($id = '') $secode = []; $secode['verify_code'] = $code; // 把校验码保存到session $secode['verify_time'] = time(); // 验证码创建时间 - // Session::set($key . $id, $secode, ''); - //保存验证码到缓存 - $options = [ - // 缓存类型为File - 'type' => 'File', - // 缓存有效期为永久有效 - 'expire'=> 0, - //缓存前缀 - 'prefix'=> 'think', - // 指定缓存目录 - 'path' => App::getAppPath().'runtime/cache/', - ]; - Cache::connect($options); - Cache::set($key . $id, $secode, $this->expire); + Session::set($key . $id, $secode, ''); ob_start(); // 输出图像