There is no license information available for the latest version (dev-master) of this package.

dev-master 2020-04-05 13:40 UTC

This package is auto-updated.

Last update: 2022-01-05 17:33:42 UTC


README

Github Workflow Status Coverage Status Scrutinizer Code Quality SensioLabs Insight Github Issues

Packagist Packagist Release Packagist Downloads

By shirshak55 with <

Please wait for v1 to use. Currently in development. If everything goes ok should be released before end of this month

What is hCaptcha?

hCaptcha is a is a CAPTCHA-like system designed to establish that a computer user is human. The team behind hCaptcha has decades of software and ML expertise and they build and operate massively scalable systems to tackle today's hardest problems.

New hCaptcha

Features

  • Framework agnostic package.
  • Well supported
  • Easy setup & configuration.
  • Well documented & IDE Friendly.
  • Well tested with maximum code quality.
  • Laravel 7.x supported.
  • PSR-7 Support (ServerRequest verification).
  • Made with ❤️ & ☕.

Support

Like Spatie I am only supporting above php 7.4 and Laravel Version above 7.

Steps

  1. Installation and Setup

To use hcaptcha, 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 hcaptcha.

For more details, check the official documentation.

You can install this package via Composer by running this command composer require laravel-hcaptcha/hcaptcha.

NOTE : The package will automatically register itself if you're using Laravel >= v5.5, so you can skip this section.

Once the package is installed, you can register the service provider in config/app.php in the providers array:

'providers' => [
    ...
    LaravelHcaptcha\Hcaptcha\HCaptchaServiceProvider::class,
],
  1. Configuration
// Edit your .env file by adding this two lines and fill it with your keys.

HCAPTCHA_SECRET=your-secret-key
HCAPTCHA_SITEKEY=your-site-key

In Laravel you can publish (But is not required usually)

Run php artisan vendor:publish --provider="LaravelHCaptcha\Hcaptcha\HcaptchaServiceProvider" to publish the config file.

Edit the secret and sitekey values in config/hcaptcha.php file: 3. Usage

Example in vanilla php

<?php

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

use LaravelHcaptcha\HCaptcha\HCaptcha;

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

if ($_POST) {
    // You need to check also if the $_POST['g-recaptcha-response'] is not empty.
    $response = $captcha->verify($_POST['h-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();
?>

Laravel

Views

Insert hcaptcha inside your form using one of this examples:

<form ...>
    // Other inputs...
    {!! hcaptcha()->display()->toHtml() !!}
    <input type
</form>

{{ hcaptcha()->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 LaravelHCaptcha\HCaptcha\CaptchaRule;

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

Contribution

Any ideas are welcome. Feel free to submit any issues or pull requests, please check the contribution guidelines.

Security

If you discover any security related issues, please email shirshak55@gmail.com instead of using the issue tracker.

Credits