vtmdev / captcha
Laravel Captcha 包
Requires
- php: ^7.2|^8.0
- ext-gd: *
- illuminate/config: ~5|^6|^7|^8|^9|^10.0
- illuminate/filesystem: ~5|^6|^7|^8|^9|^10.0
- illuminate/hashing: ~5|^6|^7|^8|^9|^10.0
- illuminate/session: ~5|^6|^7|^8|^9|^10.0
- illuminate/support: ~5|^6|^7|^8|^9|^10.0
- intervention/image: ~2.5
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^8.5|^9.0
This package is auto-updated.
Last update: 2024-09-18 05:57:25 UTC
README
一个简单的 Laravel 5/6 服务提供者,用于包含 Captcha for Laravel。
针对 Laravel 4 Captcha for Laravel Laravel 4
预览
安装
Captcha 服务提供者可以通过 Composer 安装,在项目的 composer.json
中需要 vtmdev/captcha
包并设置 minimum-stability
为 dev
(对于 Laravel 5 是必需的)。
{ "require": { "laravel/framework": "5.0.*", "vtmdev/captcha": "~2.0" }, "minimum-stability": "dev" }
或者
使用 composer 安装此包
composer require vtmdev/captcha
使用 composer update
更新包或使用 composer install
安装。
在 Windows 上,您需要在 php.ini 中包含 GD2 DLL php_gd2.dll
。您还需要包含 php_fileinfo.dll
和 php_mbstring.dll
以满足 vtmdev/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, //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; });
无状态模式
您从此 URL 获取密钥和图片 https:///captcha/api/math
并使用此方法验证 captcha
//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');
等等。
^_^