Skip to content

🛂 Helper for Google's new noCAPTCHA (reCAPTCHA v2 & v3)

License

Notifications You must be signed in to change notification settings

ARCANEDEV/noCAPTCHA

Folders and files

NameName
Last commit message
Last commit date
Jan 18, 2015
May 22, 2015
May 22, 2015
Jan 17, 2015
Jan 17, 2015
May 22, 2015
May 22, 2015
Jan 17, 2015
May 22, 2015
May 22, 2015
Jan 17, 2015

Repository files navigation

noCAPTCHA (new reCAPTCHA) Packagist License

Travis Status Coverage Status Scrutinizer Code Quality Github Release Packagist Downloads Github Issues

By ARCANEDEV©

What is reCAPTCHA?

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.

New Google reCAPTCHA

Requirements

- 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.

Version Compatibility

Laravel noCaptcha Travis
Laravel v4.2.x noCaptcha v1.2.x Travis Build v1.2.x
Laravel v5.0.x noCaptcha v2.0.x Travis Build v2.0.x

INSTALLATION

Composer

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.

Laravel

Setup

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',
],

Configuration

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.

Option 1:

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'
];
Option 2:

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(),
];
To :
<?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'
];

USAGE

Hard Coded

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.

Laravel

Views

Insert reCAPTCHA inside your form using one of this examples:

By using Blade syntax
{{ Form::open([...]) }}
    // Other inputs... 
    {{ Form::captcha() }}
    {{ Form::submit('Submit') }}
{{ Form::close() }}

// Remember what your mother told you
{{ Captcha::script() }}
OR
{{ Form::open([...]) }}
    // Other inputs... 
    {{ Captcha::display() }}
    {{ Form::submit('Submit') }}
{{ Form::close() }}

// Same thing
{{ Captcha::script() }}
Without using Blade syntax
<?php
echo Form::open([...]);
    // Other inputs... 
    echo Form::captcha();
    echo Form::submit('Submit');
echo Form::close();
?>

<?php echo Captcha::script(); ?>

Back-end (Controller or somewhere in your project ...)

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
}

Contribution

Any ideas are welcome. Feel free the submit any issues or pull requests.

TODOS:

  • Documentation
  • Examples
  • More tests and code coverage
  • Laravel Support (v4.2)
  • Laravel Support (v5.0)
  • Refactoring