anhskohbo/no-captcha
Composer 安装命令:
composer require anhskohbo/no-captcha
包简介
No CAPTCHA reCAPTCHA For Laravel.
README 文档
README
For Laravel 4 use v1 branch.
Installation
composer require anhskohbo/no-captcha
Laravel 5 and above
Setup
NOTE This package supports the auto-discovery feature of Laravel 5.5 and above, So skip these Setup instructions if you're using Laravel 5.5 and above.
In app/config/app.php add the following :
1- The ServiceProvider to the providers array :
Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class,
2- The class alias to the aliases array :
'NoCaptcha' => Anhskohbo\NoCaptcha\Facades\NoCaptcha::class,
3- Publish the config file
php artisan vendor:publish --provider="Anhskohbo\NoCaptcha\NoCaptchaServiceProvider"
Configuration
Add NOCAPTCHA_SECRET and NOCAPTCHA_SITEKEY in .env file :
NOCAPTCHA_SECRET=secret-key
NOCAPTCHA_SITEKEY=site-key
(You can obtain them from here)
Usage
Init js source
With default options :
{!! NoCaptcha::renderJs() !!}
With language support or onloadCallback option :
{!! NoCaptcha::renderJs('fr', true, 'recaptchaCallback') !!}
Display reCAPTCHA
Default widget :
{!! NoCaptcha::display() !!}
With custom attributes (theme, size, callback ...) :
{!! NoCaptcha::display(['data-theme' => 'dark']) !!}
Invisible reCAPTCHA using a submit button:
{!! NoCaptcha::displaySubmit('my-form-id', 'submit now!', ['data-theme' => 'dark']) !!}
Notice that the id of the form is required in this method to let the autogenerated callback submit the form on a successful captcha verification.
Validation
Add 'g-recaptcha-response' => 'required|captcha' to rules array :
$validate = Validator::make(Input::all(), [ 'g-recaptcha-response' => 'required|captcha' ]);
Custom Validation Message
Add the following values to the custom array in the validation language file :
'custom' => [ 'g-recaptcha-response' => [ 'required' => 'Please verify that you are not a robot.', 'captcha' => 'Captcha error! try again later or contact site admin.', ], ],
Then check for captcha errors in the Form :
@if ($errors->has('g-recaptcha-response')) <span class="help-block"> <strong>{{ $errors->first('g-recaptcha-response') }}</strong> </span> @endif
Testing
When using the Laravel Testing functionality, you will need to mock out the response for the captcha form element.
So for any form tests involving the captcha, you can do this by mocking the facade behavior:
// 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" />');
You can then test the remainder of your form as normal.
When using HTTP tests you can add the g-recaptcha-response to the request body for the 'required' validation:
// 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', ]);
Without Laravel
Checkout example below:
<?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(); ?>
Contribute
anhskohbo/no-captcha 适用场景与选型建议
anhskohbo/no-captcha 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.04M 次下载、GitHub Stars 达 1.85k, 最近一次更新时间为 2014 年 12 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「recaptcha」 「captcha」 「laravel」 「laravel4」 「laravel5」 「no-captcha」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 anhskohbo/no-captcha 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 anhskohbo/no-captcha 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 anhskohbo/no-captcha 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Client library for reCAPTCHA with proxy support, a free service that protects websites from spam and abuse.
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
A simple and easy to use Google ReCaptcha v3 package for Laravel
Two Captcha
Laravel 5 Securimage helper
CakePHP Captcha Plugin, Image, Math and Google Recaptcha Support
统计信息
- 总下载量: 9.04M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1869
- 点击次数: 26
- 依赖项目数: 44
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2014-12-03
