scarm01 / captcha
Laravel 5/6/7/8/9/10/11 验证码包
Requires
- php: ^7.2|^8.1|^8.2|^8.3
- ext-gd: *
- illuminate/config: ~5|^6|^7|^8|^9|^10|^11
- illuminate/filesystem: ~5|^6|^7|^8|^9|^10|^11
- illuminate/hashing: ~5|^6|^7|^8|^9|^10|^11
- illuminate/session: ~5|^6|^7|^8|^9|^10|^11
- illuminate/support: ~5|^6|^7|^8|^9|^10|^11
- intervention/image: ^3.7
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^8.5|^9.5.10|^10.5
This package is auto-updated.
Last update: 2024-09-08 08:41:47 UTC
README
一个简单的Laravel 5/6 服务提供者,用于包含Captcha for Laravel。
对于 Laravel 4 Captcha for Laravel Laravel 4
对于 Laravel 5 到 9 Captcha for Laravel Laravel 4
预览
安装
可以通过在项目的 composer.json
中要求 mews/captcha
包并将 minimum-stability
设置为 dev
(Laravel 5 所需)来通过 Composer 安装 Captcha 服务提供者。
{ "require": { "laravel/framework": "5.0.*", "mews/captcha": "~3.0" }, "minimum-stability": "stable" }
或者
使用 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, ]
对于 Laravel 11:您不需要添加别名,它将自动添加。
配置
自定义设置
要使用您自己的设置,请发布配置。
$ php artisan vendor:publish
config/captcha.php
return [ 'default' => [ 'length' => 5, 'width' => 120, 'height' => 36, 'quality' => 90, 'math' => true, //Enable Math Captcha 'expire' => 60, //Captcha expiration ], // ... ];
禁用验证
要禁用验证码验证,请使用 CAPTCHA_DISABLE
环境变量。例如,在 .env 配置中
CAPTCHA_DISABLE=true
示例使用
会话模式
// [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; });
在 Laravel 方式视图文件中的详细示例
//register.blade.php <img src="{{ captcha_src() }}" alt="captcha"> <div class="mt-2"></div> <input type="text" name="captcha" class="form-control @error('captcha') is-invalid @enderror" placeholder="Please Insert Captch" > @error('captcha') <div class="invalid-feedback">{{ $message }}</div> @enderror
控制器文件
Validator::make($input, [ 'name' => ['required', 'string', 'max:255'], 'email' => [ 'required', 'string', 'email', 'max:255', Rule::unique(User::class), ], 'password' => $this->passwordRules(), 'captcha' => 'required|captcha' ])->validate();
无状态模式
您可以从此 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') . ',math']; $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');
等等。
^_^