Skip to content

Latest commit

 

History

History
262 lines (187 loc) · 6.2 KB

3-Usage.md

File metadata and controls

262 lines (187 loc) · 6.2 KB

3. Usage

Table of contents

  1. Installation and Setup
  2. Configuration
  3. Usage
  4. Extras
  5. FAQ

Hard Coded (Any PHP Project)

Checkout example below:

<?php

require_once(__DIR__.'/vendor/autoload.php');

use Arcanedev\NoCaptcha\NoCaptchaV2;

$secret  = 'your-secret-key';
$sitekey = 'your-site-key';
$captcha = new NoCaptchaV2($secret, $sitekey);

if ($_POST) {
    // You need to check also if the $_POST['g-recaptcha-response'] is not empty.
    $response = $captcha->verify($_POST['g-recaptcha-response'] ?? null);

    echo $response->isSuccess()
        ? '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 programmer and you listen to your mother)
echo $captcha->script();
?>

Note: The NoCaptcha constructor accepts four arguments:

Argument Required Description
$secret Yes Your secret key.
$siteKey Yes Your site key.
$lang No You can specify your language.
$attributes No You can specify a global attributes for your captchas.

Check the examples folder for more usage details.

Invisible Captcha

The code below explains how to enable and customize the invisible reCAPTCHA on your webpage.

require_once(__DIR__ . '/../vendor/autoload.php');

use Arcanedev\NoCaptcha\NoCaptchaV2;

$secret  = 'your-secret-key';
$sitekey = 'your-site-key';
$captcha = new NoCaptchaV2($secret, $sitekey);

if ($_POST) {
    // You need to check also if the $_POST['g-recaptcha-response'] is not empty.
    $response = $captcha->verify($_POST['g-recaptcha-response'] ?? null);

    echo $response->isSuccess()
        ? 'Yay ! You are a human.'
        : 'No ! You are a robot.';

    exit();
}

?>

<form method="POST" id="demo-form">
    <?php echo $captcha->button('Send', ['data-badge' => 'inline']); ?>
</form>

<?php echo $captcha->script(); ?>

<script>
    function onSubmit(token) {
        document.getElementById("demo-form").submit();
    }
</script>

NOTE : You need to specify the invisible version in your captcha admin page. Check this page for more details: https://developers.google.com/recaptcha/docs/versions

Version 3

The code below shows you how to use the ReCaptcha V3:

<?php

require_once(__DIR__.'/vendor/autoload.php');

use Arcanedev\NoCaptcha\NoCaptchaV3;

$captcha = new NoCaptchaV3(
    'SECRET-KEY',
    'SITE-KEY'
);

if ($_POST) {
    $response = $captcha->verify($_POST['g-recaptcha-response'] ?? null);

    echo $response->isSuccess()
        ? 'Yay ! You are a human.'
        : 'No ! You are a robot.';

    exit();
}

?>

<form method="POST">
    <input type="email" name="email"><br>
    <button type="submit">Submit</button>

    <?php echo $captcha->input('g-recaptcha-response'); ?>
</form>

<?php echo $captcha->script(); ?>

<script>
    grecaptcha.ready(function() {
        grecaptcha.execute('SITE-KEY', {action: 'homepage'});
    });
</script>

Laravel

Views

Insert reCAPTCHA inside your form using one of this examples:

By using Blade syntax
{!! Form::open([...]) !!}
    // Other inputs...
    {!! no_captcha()->display() !!}
    {!! Form::submit('Submit') !!}
{!! Form::close() !!}

// Remember what your mother told you
{!! no_captcha()->script() !!}

For Laravel 4.2, use {{ ... }} instead of {!! ... !!}

Without using Blade syntax
<?php

echo Form::open([...]);
    // Other inputs...
    echo no_captcha()->display()->toHtml();
    echo Form::submit('Submit');
echo Form::close();

?>

<?php echo no_captcha()->script()->toHtml(); ?>

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:

use Arcanedev\NoCaptcha\Rules\CaptchaRule;

$inputs   = Input::all();
$rules    = [
    // Other validation rules...
    'g-recaptcha-response' => ['required', new CaptchaRule],
];
$messages = [
    'g-recaptcha-response.required' => 'Your custom validation message.',
    '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:

// resources/lang/en/validation.php
return [
    ...
    // Add this line with your custom message
    'captcha'   => "If you read this message, then you're a robot.",
];
// resources/lang/fr/validation.php
return [
    ...
    // Ajoutez cette ligne avec votre message personnalisé
    'captcha'   => 'Si vous lisez ce message, alors vous êtes un robot.',
];

For the required rule, you can customize it by adding your messages to custom array in the resources/lang/xx/validation.php:

'custom' => [
    'g-recaptcha-response' => [
        'required' => 'Your custom validation message for captchas.',
    ],
],

For Laravel 4.2, the lang folder is located in app/lang

use Arcanedev\NoCaptcha\Rules\CaptchaRule;

$validator = Validator::make(Input::all(), [
    // Other validation rules...
    'g-recaptcha-response' => ['required', new CaptchaRule],
]);

if ($validator->fails()) {
    $errors = $validator->messages();

    var_dump($errors->first('g-recaptcha-response'));

    // Redirect back or throw an error
}

For more advanced usage, check the official recaptcha documentation.