masoud5070 / captcha
Laravel 5 & 6 验证码包
Requires
- php: ^7.2|^8.0
- ext-gd: *
- illuminate/config: ~5|^6|^7|^8
- illuminate/filesystem: ~5|^6|^7|^8
- illuminate/hashing: ~5|^6|^7|^8
- illuminate/session: ~5|^6|^7|^8
- illuminate/support: ~5|^6|^7|^8
- intervention/image: ~2.5
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^8.5
- dev-master
- 3.3.0
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.2
- 3.0.1
- 3.0.0
- 2.3.0
- 2.2.9
- 2.2.8
- 2.2.7
- 2.2.6
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.8
- 2.1.7
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.0.2
- v1.0.1
- dev-dev
- dev-feature/custom-route
- dev-revert-146-master
- dev-master-l4
This package is auto-updated.
Last update: 2024-09-06 18:43:52 UTC
README
一个简单的Laravel 5/6服务提供者,用于包含Laravel 验证码。
针对 Laravel 4 Laravel 4 验证码
预览
安装
可以通过Composer安装 Captcha 服务提供者,在项目的 composer.json
中添加 Masoud5070/captcha
包,并将 minimum-stability
设置为 dev
(Laravel 5 所需)。
{ "require": { "laravel/framework": "5.0.*", "Masoud5070/captcha": "~2.0" }, "minimum-stability": "dev" }
或者
使用 composer 添加此包
composer require masoud5070/captcha
使用 composer update
更新包或使用 composer install
安装。
在 Windows 上,您需要在 php.ini 中包含 GD2 DLL php_gd2.dll
。并且您还需要包含 php_fileinfo.dll
和 php_mbstring.dll
以满足 Masoud5070/captcha
依赖项的要求。
用法
要使用 Captcha 服务提供者,您必须在启动 Laravel 应用程序时注册提供者。这主要有两种方式。
在 config/app.php
中找到 providers
键并注册 Captcha 服务提供者。
'providers' => [ // ... 'Masoud5070\Captcha\CaptchaServiceProvider', ]
针对 Laravel 5.1+
'providers' => [ // ... Masoud5070\Captcha\CaptchaServiceProvider::class, ]
在 config/app.php
中找到 aliases
键。
'aliases' => [ // ... 'Captcha' => 'Masoud5070\Captcha\Facades\Captcha', ]
针对 Laravel 5.1+
'aliases' => [ // ... 'Captcha' => Masoud5070\Captcha\Facades\Captcha::class, ]
配置
要使用自己的设置,请发布配置。
$ php artisan vendor:publish
config/captcha.php
return [ 'default' => [ 'length' => 5, 'width' => 120, 'height' => 36, 'quality' => 90, 'math' => true, //Enable Math Captcha 'expire' => 60, //Stateless/API captcha expiration ], // ... ];
示例用法
会话模式
// [your site path]/Http/routes.php Route::any('captcha-test', function() { if (request()->getMethod() == 'POST') { $rules = ['captcha' => 'required|captcha']; $validator = validator()->make(request()->all(), $rules); if ($validator->fails()) { echo '<p style="color: #ff0000;">Incorrect!</p>'; } else { echo '<p style="color: #00ff30;">Matched :)</p>'; } } $form = '<form method="post" action="captcha-test">'; $form .= '<input type="hidden" name="_token" value="' . csrf_token() . '">'; $form .= '<p>' . captcha_img() . '</p>'; $form .= '<p><input type="text" name="captcha"></p>'; $form .= '<p><button type="submit" name="check">Check</button></p>'; $form .= '</form>'; return $form; });
无状态模式
您从此 URL 获取密钥和图片 https:///captcha/api/math
,并使用此方法验证验证码
//key is the one that you got from json response // fix validator // $rules = ['captcha' => 'required|captcha_api:'. request('key')]; $rules = ['captcha' => 'required|captcha_api:'. request('key') . ',default']; $validator = validator()->make(request()->all(), $rules); if ($validator->fails()) { return response()->json([ 'message' => 'invalid captcha', ]); } else { //do the job }
返回图片
captcha();
或者
Captcha::create();
返回 URL
captcha_src();
或者
Captcha::src('default');
返回 HTML
captcha_img();
或者
Captcha::img();
要使用不同的配置
captcha_img('flat'); Captcha::img('inverse');
等等。
^_^