anhskohbo / no-captcha
Laravel 的无 CAPTCHA reCAPTCHA。
3.6.0
2024-03-13 02:22 UTC
Requires
- php: >=5.5.5
- guzzlehttp/guzzle: ^6.2|^7.0
- illuminate/support: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- phpunit/phpunit: ~4.8|^9.5.10|^10.5
README
对于 Laravel 4,请使用 v1 分支。
安装
composer require anhskohbo/no-captcha
Laravel 5 及以上版本
设置
注意 此包支持 Laravel 5.5 及以上版本的自动发现功能,因此如果您使用 Laravel 5.5 及以上版本,请跳过这些 设置
指令。
在 app/config/app.php
中添加以下内容
1- ServiceProvider 到 providers 数组
Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class,
2- 类别名到别名数组
'NoCaptcha' => Anhskohbo\NoCaptcha\Facades\NoCaptcha::class,
3- 发布配置文件
php artisan vendor:publish --provider="Anhskohbo\NoCaptcha\NoCaptchaServiceProvider"
配置
在 .env 文件中添加 NOCAPTCHA_SECRET
和 NOCAPTCHA_SITEKEY
NOCAPTCHA_SECRET=secret-key
NOCAPTCHA_SITEKEY=site-key
(您可以从 此处 获取它们)
使用方法
初始化 js 源
使用默认选项
{!! NoCaptcha::renderJs() !!}
使用 语言支持 或 onloadCallback 选项
{!! NoCaptcha::renderJs('fr', true, 'recaptchaCallback') !!}
显示 reCAPTCHA
默认小部件
{!! NoCaptcha::display() !!}
使用 自定义属性(主题、大小、回调等)
{!! NoCaptcha::display(['data-theme' => 'dark']) !!}
使用 提交按钮 的不可见 reCAPTCHA
{!! NoCaptcha::displaySubmit('my-form-id', 'submit now!', ['data-theme' => 'dark']) !!}
请注意,在此方法中需要表单的 id,以便自动生成的回调在成功的 captcha 验证后提交表单。
验证
将 'g-recaptcha-response' => 'required|captcha'
添加到规则数组
$validate = Validator::make(Input::all(), [ 'g-recaptcha-response' => 'required|captcha' ]);
自定义验证消息
将以下值添加到 validation
语言文件中的 custom
数组
'custom' => [ 'g-recaptcha-response' => [ 'required' => 'Please verify that you are not a robot.', 'captcha' => 'Captcha error! try again later or contact site admin.', ], ],
然后在 Form
中检查 captcha 错误
@if ($errors->has('g-recaptcha-response')) <span class="help-block"> <strong>{{ $errors->first('g-recaptcha-response') }}</strong> </span> @endif
测试
当使用 Laravel 测试功能 时,您需要模拟 captcha 表单元素的响应。
因此,对于涉及 captcha 的任何表单测试,您可以通过模拟外观行为来这样做
// prevent validation error on captcha NoCaptcha::shouldReceive('verifyResponse') ->once() ->andReturn(true); // provide hidden input for your 'required' validation NoCaptcha::shouldReceive('display') ->zeroOrMoreTimes() ->andReturn('<input type="hidden" name="g-recaptcha-response" value="1" />');
然后您可以根据正常方式测试表单的其余部分。
当使用 HTTP 测试时,您可以将 g-recaptcha-response
添加到请求体以进行 'required' 验证
// prevent validation error on captcha NoCaptcha::shouldReceive('verifyResponse') ->once() ->andReturn(true); // POST request, with request body including g-recaptcha-response $response = $this->json('POST', '/register', [ 'g-recaptcha-response' => '1', 'name' => 'John', 'email' => 'john@example.com', 'password' => '123456', 'password_confirmation' => '123456', ]);
不使用 Laravel
查看以下示例
<?php require_once "vendor/autoload.php"; $secret = 'CAPTCHA-SECRET'; $sitekey = 'CAPTCHA-SITEKEY'; $captcha = new \Anhskohbo\NoCaptcha\NoCaptcha($secret, $sitekey); if (! empty($_POST)) { var_dump($captcha->verifyResponse($_POST['g-recaptcha-response'])); exit(); } ?> <form action="?" method="POST"> <?php echo $captcha->display(); ?> <button type="submit">Submit</button> </form> <?php echo $captcha->renderJs(); ?>