A Laravel package to seamlessly integrate Google reCAPTCHA v3, v2 or hCAPTCHA into your application forms or Restful API.
Service | Version | Type | Supported |
---|---|---|---|
Google reCAPTCHA | v3 | - | ✅ Yes |
Google reCAPTCHA | v2 | Checkbox | ✅ Yes |
Google reCAPTCHA | v2 | Invisible | ✅ Yes |
hCAPTCHA | - | Checkbox | ✅ Yes |
hCAPTCHA | - | Invisible | ✅ Yes |
Note: This package requires PHP 7.4 or higher.
You can install the package via composer:
composer require martian/laracaptcha
Add the following to the providers
array in config/app.php
:
Martian\LaraCaptcha\Providers\LaraCaptchaServiceProvider::class,
Publish the configuration file using the following command:
php artisan vendor:publish --provider="Martian\LaraCaptcha\Providers\LaraCaptchaServiceProvider"
The configuration file is located at config/laracaptcha.php
. The following options are available:
-
In order to use reCAPTCHA you need to register your site for an API key pair. To use reCaptcha v2 Checkbox, select Challenge (v2) > I'm not a robot Checkbox. To use the invisible reCAPTCHA, select Challenge (v2) > Invisible reCAPTCHA badge. The API key pair consists of a site key and secret key. Set the
default
option torecaptcha
inconfig/laracaptcha.php
:'default' => 'recaptcha',
-
Change the
version
option tov2
to use reCAPTCHA v2:'drivers' => [ 'recaptcha' => [ ... 'version' => 'v2', ... ], ],
-
Add
RECAPTCHA_SITE_KEY
andRECAPTCHA_SECRET_KEY
to your.env
file:RECAPTCHA_SITE_KEY=your-site-key RECAPTCHA_SECRET_KEY=your-secret-key
-
In order to use reCAPTCHA you need to register your site for an API key pair. To use reCaptcha v3, select reCAPTCHA v3. The API key pair consists of a site key and secret key. Set the
default
option torecaptcha
inconfig/laracaptcha.php
:'default' => 'recaptcha',
-
Change the
version
option tov3
to use reCAPTCHA v3:'drivers' => [ 'recaptcha' => [ ... 'version' => 'v3', ... ], ],
-
Add
RECAPTCHA_SITE_KEY
,RECAPTCHA_SECRET_KEY
andRECAPTCHA_SITE_URL
to your.env
file:RECAPTCHA_SITE_KEY=your-site-key RECAPTCHA_SECRET_KEY=your-secret-key RECAPTCHA_SITE_URL=${APP_URL}
-
In order to use hCAPTCHA you need to register your site for an API key pair. The API key pair consists of a site key and secret key. Set the
default
option tohcaptcha
inconfig/laracaptcha.php
:'default' => 'hcaptcha',
-
Add
HCAPTCHA_SITE_KEY
andHCAPTCHA_SECRET_KEY
to your.env
file:HCAPTCHA_SITE_KEY=10000000-ffff-ffff-ffff-000000000001 HCAPTCHA_SECRET_KEY=0x0000000000000000000000000000000000000000
These are the test keys we use by default. You should not use them in production!
To display captcha in your form, follow the steps below according to the captcha configuration you are using.
Add the following to the <head>
section of your page:
{!! LaraCaptcha::script() !!}
With other options in Google reCaptcha v2 Checkbox dox
{!! LaraCaptcha::script('yourCallbackFunction', 'explicit', 'en') !!}
Note: The first parameter is the callback function name, the second is the rendering mode (explicit or onload), and the third is the language code from doc
Add the following to your form:
{!! LaraCaptcha::display() !!}
With other options in Google reCaptcha v2 Checkbox dox
{!! LaraCaptcha::display(['data-theme' => 'dark']) !!}
Note: The parameter is an array of attributes for the widget
Add the following to your form:
{!! LaraCaptcha::displayInvisibleButton('formIdentifier', 'Submit Button',['data-size' => 'invisible']) !!}
Note: The first parameter is the form identifier, the second is the button label (Submit Button), and the third is an array of attributes for the widget, see doc. Add the formIdentifier value as the id in the form element
Add the following to your validation rules:
'g-recaptcha-response' => 'recaptcha',
You can also use the rule in the Validator facade:
$validator = Validator::make($request->all(), [
'g-recaptcha-response' => 'recaptcha',
]);
Add the following to your validation messages:
'g-recaptcha-response.recaptcha' => 'Captcha verification failed.',
Or you can change the default message in config/laracaptcha.php
:
'error_message' => 'Captcha verification failed.',
Add the following to the <head>
section of your page:
{!! LaraCaptcha::script() !!}
With other options in Google reCaptcha v3 dox
{!! LaraCaptcha::script('yourCallbackFunction', 'explicit', 'en') !!}
Add the following to your form:
{!! LaraCaptcha::display() !!}
With other options in Google reCaptcha v3 dox
{!! LaraCaptcha::display(['action' => 'homepage', 'custom_validation' => 'yourCustomFunction', 'recaptcha_input_identifier' => 'yourReCaptchaInputId']) !!}
Note: The parameter is an array of attributes for the widget, see doc for actions type
Add the following to the <head>
section of your page:
{!! LaraCaptcha::script() !!}
With other options in hCAPTCHA dox
{!! LaraCaptcha::script('yourCallbackFunction', 'onload' 'en', 'on') !!}
Note: The first parameter is the callback function name, the second is the rendering mode (onload or explicit), the third is the language code from doc, and the fourth is the recaptchacompat option
Add the following to your form:
{!! LaraCaptcha::display() !!}
With other options in hCAPTCHA dox
{!! LaraCaptcha::display(['data-theme' => 'dark']) !!}
Note: The parameter is an array of attributes for the widget
Add the following to your form, see documentation for invisible hcaptcha:
{!! LaraCaptcha::displayInvisibleButton('formIdentifier', 'Submit Button',['data-size' => 'invisible']) !!}
Note: The first parameter is the form identifier, the second is the button label (Submit Button), and the third is an array of attributes for the widget, see doc
Add the following to your validation rules:
'h-captcha-response' => 'hcaptcha',
You can also use the rule in the Validator facade:
$validator = Validator::make($request->all(), [
'h-captcha-response' => 'hcaptcha',
]);
Add the following to your validation messages:
'h-captcha-response.hcaptcha' => 'Captcha verification failed.',
Or you can change the default message in config/laracaptcha.php
:
'error_message' => 'Captcha verification failed.',
For other configuration go through the config/laracaptcha.php
file.
./vendor/bin/phpunit
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email hendurhance.dev@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.