abcdebo / laravel-google-recaptcha
为Laravel提供的Google Recaptcha。
dev-master
2024-01-30 19:06 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
Requires (Dev)
- phpunit/phpunit: ~4.8|^9.5.10
This package is auto-updated.
Last update: 2024-09-30 02:02:56 UTC
README
安装
composer require abcdebo/laravel-google-recaptcha
Laravel 5及以上
设置
注意 此包支持Laravel 5.5及以上的自动发现功能,因此如果您使用Laravel 5.5及以上版本,请跳过以下 设置
指令。
在 app/config/app.php
中添加以下内容:
1- 将 ServiceProvider 添加到 providers 数组
ABCDebo\GoogleRecaptcha\GoogleRecaptchaServiceProvider::class,
2- 将类别名添加到别名数组
'GoogleRecaptcha' => ABCDebo\GoogleRecaptcha\Facades\GoogleRecaptcha::class,
3- 发布配置文件
php artisan vendor:publish --provider="ABCDebo\GoogleRecaptcha\GoogleRecaptchaServiceProvider"
配置
在 .env 文件中添加 GoogleRecaptcha_SECRET
和 GoogleRecaptcha_SITEKEY
GoogleRecaptcha_SECRET=secret-key
GoogleRecaptcha_SITEKEY=site-key
(您可以从这里获取它们)
使用方法
初始化js源
使用默认选项
{!! GoogleRecaptcha::renderJs() !!}
使用语言支持或onloadCallback选项
{!! GoogleRecaptcha::renderJs('fr', true, 'recaptchaCallback') !!}
显示reCAPTCHA
默认小部件
{!! GoogleRecaptcha::display() !!}
使用自定义属性(主题、大小、回调等)
{!! GoogleRecaptcha::display(['data-theme' => 'dark']) !!}
使用提交按钮的不可见reCAPTCHA
{!! GoogleRecaptcha::displaySubmit('my-form-id', 'submit now!', ['data-theme' => 'dark']) !!}
注意,在此方法中,表单的id是必需的,以便自动生成的回调在成功的验证码验证后提交表单。
验证
将 '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
中检查验证码错误
@if ($errors->has('g-recaptcha-response')) <span class="help-block"> <strong>{{ $errors->first('g-recaptcha-response') }}</strong> </span> @endif
测试
当使用Laravel测试功能时,您需要模拟验证码表单元素的响应。
因此,对于涉及验证码的任何表单测试,您可以通过模拟外观行为来实现这一点
// prevent validation error on captcha GoogleRecaptcha::shouldReceive('verifyResponse') ->once() ->andReturn(true); // provide hidden input for your 'required' validation GoogleRecaptcha::shouldReceive('display') ->zeroOrMoreTimes() ->andReturn('<input type="hidden" name="g-recaptcha-response" value="1" />');
然后您可以根据正常流程测试表单的其余部分。
当使用HTTP测试时,您可以将 g-recaptcha-response
添加到请求体以进行 'required' 验证
// prevent validation error on captcha GoogleRecaptcha::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 \ABCDebo\GoogleRecaptcha\GoogleRecaptcha($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(); ?>