By ARCANEDEV©
reCAPTCHA is a free service that protects your site from spam and abuse. It uses advanced risk analysis engine to tell humans and bots apart. With the new API, a significant number of your valid human users will pass the reCAPTCHA challenge without having to solve a CAPTCHA. reCAPTCHA comes in the form of a widget that you can easily add to your blog, forum, registration form, etc.
- PHP >= 5.4.0
- ext-curl: *
- ext-json: *
To use reCAPTCHA, you need to have a site key
and a secret key
. Click here to setup a domain and get your keys.
The site key
is using for the widget and the secret key
is used to validate the response we get from Google.
For more details, check the official documentation.
Laravel | noCaptcha | Travis |
---|---|---|
You can install this package via Composer. Add this to your composer.json
:
{
"require": {
"arcanedev/no-captcha": "~1.2"
}
}
Then install it via composer install
or composer update
.
Once the package is installed, you can register the service provider in app/config/app.php
in the providers
array:
'providers' => [
...
'Arcanedev\NoCaptcha\Laravel\ServiceProvider',
],
And the facade in the aliases
array:
'aliases' => [
...
'Captcha' => 'Arcanedev\NoCaptcha\Laravel\Facade',
],
There is not really a need to publish the configuration file. Both the secret
and sitekey
should be set in your environment file so it won't be available in your versioning system.
See Protecting Sensitive Configuration if you don't know how to setup environment variables in Laravel >= v4.1
.
<?php
/**
* Edit your .env.php or .env.*.php (If you know what i mean)
* By adding this two lines and fill it with your keys.
*/
return [
'NOCAPTCHA_SECRET' => 'your-secret-key',
'NOCAPTCHA_SITEKEY' => 'your-site-key'
];
Run php artisan config:publish arcanedev/no-captcha
to publish the config file.
Edit the secret
and sitekey
values in app/config/packages/arcanedev/no-captcha/config.php
file:
<?php
return [
'secret' => getenv('NOCAPTCHA_SECRET') ?: 'no-captcha-secret',
'sitekey' => getenv('NOCAPTCHA_SITEKEY') ?: 'no-captcha-sitekey',
'lang' => app()->getLocale(),
];
<?php
return [
'secret' => 'your-secret-key',
'sitekey' => 'your-site-key',
'lang' => 'en', // You can remove this line, it take the lang from config/app.php => 'locale'
];
Checkout example below:
<?php
require_once "vendor/autoload.php";
use Arcanedev\NoCaptcha\NoCaptcha;
$secret = 'your-secret-key';
$sitekey = 'your-site-key';
$captcha = new NoCaptcha($secret, $sitekey);
if ( ! empty($_POST)) {
$response = $_POST['g-recaptcha-response'];
$result = $captcha->verify($response);
echo $result === true
? 'Yay ! You are a human.'
: 'No ! You are a robot.';
exit();
}
?>
<form action="?" method="POST">
<?php echo $captcha->display(); ?>
<button type="submit">Submit</button>
</form>
<?php
// At the bottom, before the </body> (If you're a good boy and you listen to your mother)
echo $captcha->script();
?>
Check the examples folder for more usage details.
Insert reCAPTCHA inside your form using one of this examples:
{{ Form::open([...]) }}
// Other inputs...
{{ Form::captcha() }}
{{ Form::submit('Submit') }}
{{ Form::close() }}
// Remember what your mother told you
{{ Captcha::script() }}
{{ Form::open([...]) }}
// Other inputs...
{{ Captcha::display() }}
{{ Form::submit('Submit') }}
{{ Form::close() }}
// Same thing
{{ Captcha::script() }}
<?php
echo Form::open([...]);
// Other inputs...
echo Form::captcha();
echo Form::submit('Submit');
echo Form::close();
?>
<?php echo Captcha::script(); ?>
To validate the response we get from Google, your can use the captcha
rule in your validator:
$inputs = Input::all();
$rules = [
// Other validation rules...
'g-recaptcha-response' => 'captcha',
];
$messages = [
'g-recaptcha-response.captcha' => 'Your custom validation message.',
];
$validator = Validator::make($inputs, $rules, $messages);
if ($validator->fails()) {
$errors = $validator->messages();
var_dump($errors->first('g-recaptcha-response'));
// Redirect back or throw an error
}
If you want to manage the localized messages, edit the validation.php
files inside your lang directory.
For example:
// app/lang/en/validation.php
return [
...
// Add this line with your custom message
"captcha" => "If you read this message, then you're a robot.",
];
// app/lang/fr/validation.php
return [
...
// Ajoutez cette ligne avec votre message personnalisé
"captcha" => "Si vous lisez ce message, alors vous êtes un robot.",
];
$validator = Validator::make(Input::all(), [
// Other validation rules...
'g-recaptcha-response' => 'captcha',
]);
if ($validator->fails()) {
$errors = $validator->messages();
var_dump($errors->first('g-recaptcha-response'));
// Redirect back or throw an error
}
Any ideas are welcome. Feel free the submit any issues or pull requests.
- Documentation
- Examples
- More tests and code coverage
- Laravel Support (v4.2)
- Laravel Support (v5.0)
- Refactoring