timehunter/laravel-google-recaptcha-v2
最新稳定版本:v1.0.3
Composer 安装命令:
composer require timehunter/laravel-google-recaptcha-v2
包简介
Laravel Package for google reCAPTCHA v2
关键字:
README 文档
README
Laravel Package for Google reCAPTCHA v2
Welcome all tickets (features/questions/bugs/improvements), I will respond to all tickets within 48 hours.
This is a package for Google reCAPTCHA v2.
If you want to use v3, please go to: https://github.com/RyanDaDeng/laravel-google-recaptcha-v3
DEMO
Checkbox
Invisible - hidden
Invisible - Inline
Corner
Description
A Laravel package for Google reCAPTCHA v2.
If you want to make your own font-end template, you have full access to modify template file, so you can customise your own template by reading through Google official guide for either invisible badge or inline checkbox. https://developers.google.com/recaptcha/docs/display
Features
- Support invisible, corner and inline badge style
- Support multiple reCAPTCHA on the same page for different forms
- Support multiple actions to be placed on the same page
- Support custom implementation on config interface
- Support custom implementation on request method interface
- Support custom implementation on Template file
Requirement
This package requires the following dependencies:
-
Laravel 5.x
-
If you want to use Validation Class your Laravel version needs to be >= 5.5
-
php > 5
-
Please ensure that you have read basic information from Google reCAPTCHA v2.
Installation
Via Composer
$ composer require timehunter/laravel-google-recaptcha-v2 "~1.0.0" -vvv
If your Laravel framework version <= 5.4, please register the service provider in your config file: /config/app.php, otherwise please skip it.
'providers'=[ ...., TimeHunter\LaravelGoogleReCaptchaV2\Providers\GoogleReCaptchaV2ServiceProvider::class ]
And also
'aliases'=[ ...., 'GoogleReCaptchaV2'=> TimeHunter\LaravelGoogleReCaptchaV2\Facades\GoogleReCaptchaV2::class ]
If your Laravel framework version is >= 5.5, just run the following command to publish config.
$ php artisan vendor:publish --provider="TimeHunter\LaravelGoogleReCaptchaV2\Providers\GoogleReCaptchaV2ServiceProvider" --tag=googlerecaptchav2.config
Optional: if you want to modify or customise your own template, you can publish a default view first, and change 'template' in config file:
$ php artisan vendor:publish --provider="TimeHunter\LaravelGoogleReCaptchaV2\Providers\GoogleReCaptchaV2ServiceProvider" --tag=googlerecaptchav2.views
After installation, you should see a googlerecaptchav2/template.blade under views folder and googlerecaptchav2.php in your app/config folder.
Basic Usage
Setting up your Google reCAPTCHA details in config file
Please register all details on host_name, site_key, secret_key and site_verify_url.
For more details please check comments in config file.
Display reCAPTCHA v2
Note: for styling with reCAPTCHA v2 badge, the official site does not support it. You can still customise it on its div element if you want.
Blade
Include div with an ID inside your form, e.g.
<div id="form_id_1"></div> <div id="form_id_2"></div>
Include Template script in your bottom/header of your page, e.g.
{!! GoogleReCaptchaV2::render('form_id_1','form_id_2') !!}
Example Usage
{{--if laravel version <=5.6, please use {{ csrf_field() }}--}}
<form method="POST" action="/verify">
@csrf
<div id="form_1_id"></div>
<input type="submit" value="submit">
</form>
<form method="POST" action="/verify">
@csrf
<div id="form_2_id"></div>
<input type="submit" value="submit">
</form>
{!! GoogleReCaptchaV2::render('form_1_id','form_2_id') !!}
The backend request will receive a value for 'g-recaptcha-response', please take a look at Sample Use Case and Facade usage sections.
Badge Display
Importance: you can always make your own template, just assign your template in config:
[
...
'template' => 'test.template' // if your template is located at resources/views/test/template
...
]
Checkbox
- Go to config file, and set
[
...
'badge' => 'inline'
...
]
- Badge will be displayed as checkbox format within the form.
Invisible - inline
- Set size as invisible
[
...
'size' => 'invisible'
...
]
- Set badge as inline or bottomright or bottomleft
[
...
'badge' => 'inline' // also support: bottomright,bottomleft
...
]
Invisible - hidden
- Set size as invisible
[
...
'size' => 'invisible'
...
]
- Modify your div with style display:none
- Refer to Google official site: https://developers.google.com/recaptcha/docs/faq , you need to include the following text:
This site is protected by reCAPTCHA and the Google
<a href="https://policies.google.com/privacy">Privacy Policy</a> and
<a href="https://policies.google.com/terms">Terms of Service</a> apply.
Corner
- Set size as invisible
[
...
'size' => 'invisible'
...
]
- Set badge as bottomright/bottomleft
[
...
'badge' => 'bottomright'
...
]
Validation Class (Only support Laravel >= 5.5)
You can use provided Validation object to verify your reCAPTCHA.
use TimeHunter\LaravelGoogleReCaptchaV2\Validations\GoogleReCaptchaV2ValidationRule $rule = [ 'g-recaptcha-response' => [new GoogleReCaptchaV2ValidationRule()] ];
Facade Usage
You can also directly use registered service by calling the following method.
- verifyResponse() which accepts the token value from your form. This return Google reCAPTCHA Response object.
GoogleReCaptchaV2::verifyResponse($value, $ip=null);
Example Usage
GoogleReCaptchaV2::verifyResponse($value,$ip)->getMessage(); GoogleReCaptchaV2::verifyResponse($value)->isSuccess(); GoogleReCaptchaV2::verifyResponse($value)->toArray();
GoogleReCaptchaV2::verifyResponse($request->input('g-recaptcha-response'))->getMessage()
Sample Use Case
-
Register your action in config, also enable score and set up your own site key and secret key:
-
Register two routes in web.php
Route::get('/index', 'ReCaptchaController@index'); Route::post('/verify', 'ReCaptchaController@verify');
- Create two functions in controller:
public function verify(Request $request) { dd(GoogleReCaptchaV2::verifyResponse($request->input('g-recaptcha-response'))->getMessage()); } public function index(Request $request) { return view('index'); }
- Create your form in index.blade.php:
{{--if laravel version <=5.6, please use {{ csrf_field() }}--}}
<form method="POST" action="/verify">
@csrf
<div id="contact_us_id"></div>
<input type="submit" value="submit">
</form>
<form method="POST" action="/verify">
@csrf
<div id="signup_id"></div>
<input type="submit" value="submit">
</form>
{!! GoogleReCaptchaV2::render('contact_us_id','signup_id') !!}
Advanced Usage
Custom implementation on Template
After publish views, a blade file created under googlerecaptchaV2, you can customise it and change template value in config file, e.g. if your template is saved in resources/views/test/template, you should put values as below:
[
...
'template' => 'test.template'
...
]
Custom implementation on Config
For some users, they might store the config details in their own storage e.g database. You can create your own class and implement:
TimeHunter\LaravelGoogleReCaptchaV2\Interfaces\ReCaptchaConfigv2Interface
Remember to register it in your own service provider
$this->app->bind( ReCaptchaConfigV2Interface::class, YourOwnCustomImplementation::class );
Custom implementation on Request method
The package has two default options to verify: Guzzle and Curl, if you want to use your own request method, You can create your own class and implement
TimeHunter\LaravelGoogleReCaptchaV2\Interfaces\RequestClientInterface
Remember to register it in your own service provider
$this->app->bind( RequestClientInterface::class, YourOwnCustomImplementation::class );
Contributers
@markheramis
Security
If you discover any security related issues, please email ryandadeng@gmail.com instead of using the issue tracker.
License
MIT. Please see the license file for more information.
timehunter/laravel-google-recaptcha-v2 适用场景与选型建议
timehunter/laravel-google-recaptcha-v2 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 89.34k 次下载、GitHub Stars 达 13, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「recaptcha」 「laravel」 「Google ReCaptcha」 「reCAPTCHA V2」 「Laravel reCAPTCHA」 「GoogleReCaptcha」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 timehunter/laravel-google-recaptcha-v2 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 timehunter/laravel-google-recaptcha-v2 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 timehunter/laravel-google-recaptcha-v2 相关的其它包
同方向 / 同关键字的高下载量 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
Magento 2 Social Login extension is designed for quick login to your Magento 2 store without procesing complex register steps
CakePHP Captcha Plugin, Image, Math and Google Recaptcha Support
ReCaptcha form field integration for Symfony
统计信息
- 总下载量: 89.34k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 未知



