triadi-pro / captcha
Laravel 5 & 6 验证码包
3.2.4
2020-12-14 12:34 UTC
Requires
- php: ^7.2
- 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
This package is not auto-updated.
Last update: 2024-09-21 17:49:02 UTC
README
一个简单的Laravel 5/6服务提供程序,用于包含Laravel验证码。
用于Laravel 4 Laravel验证码 Laravel 4
预览
安装
可以通过Composer安装Captcha服务提供程序,要求mews/captcha包,并在项目的composer.json中将minimum-stability设置为dev(Laravel 5所需)。
{
"require": {
"laravel/framework": "5.0.*",
"mews/captcha": "~2.0"
},
"minimum-stability": "dev"
}
或
使用Composer要求此包
composer require mews/captcha
使用composer update更新您的包或使用composer install安装。
在Windows中,您需要在php.ini中包含GD2 DLL php_gd2.dll。您还需要包含php_fileinfo.dll和php_mbstring.dll以满足mews/captcha依赖项的要求。
用法
要使用Captcha服务提供程序,您必须在启动Laravel应用程序时注册提供程序。本质上有两种方法可以做到这一点。
在config/app.php中找到providers键并注册Captcha服务提供程序。
'providers' => [ // ... 'Mews\Captcha\CaptchaServiceProvider', ]
对于Laravel 5.1+
'providers' => [ // ... Mews\Captcha\CaptchaServiceProvider::class, ]
在config/app.php中找到aliases键。
'aliases' => [ // ... 'Captcha' => 'Mews\Captcha\Facades\Captcha', ]
对于Laravel 5.1+
'aliases' => [ // ... 'Captcha' => Mews\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');
等。
^_^