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
Invisible - hidden
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
- 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
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.
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.
Please register all details on host_name, site_key, secret_key and site_verify_url.
For more details please check comments in config file.
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.
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') !!}
{{--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.
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
...
]
- Go to config file, and set
[
...
'badge' => 'inline'
...
]
- Badge will be displayed as checkbox format within the form.
- 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.
- Set size as invisible
[
...
'size' => 'invisible'
...
]
- Set badge as bottomright/bottomleft
[
...
'badge' => 'bottomright'
...
]
You can use provided Validation object to verify your reCAPTCHA.
use TimeHunter\LaravelGoogleReCaptchaV2\Validations\GoogleReCaptchaV2ValidationRule
$rule = [
'g-recaptcha-response' => [new GoogleReCaptchaV2ValidationRule()]
];
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()
-
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') !!}
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'
...
]
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
);
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
);
@markheramis
If you discover any security related issues, please email ryandadeng@gmail.com instead of using the issue tracker.
MIT. Please see the license file for more information.