PHPackages                             esyede/hcaptcha-laravel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. esyede/hcaptcha-laravel

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

esyede/hcaptcha-laravel
=======================

Integrate hCaptcha into your Laravel application.

v1.0.2(1y ago)0100MITPHPPHP ^7.2|^8.0

Since Jun 26Pushed 1y ago1 watchersCompare

[ Source](https://github.com/esyede/hcaptcha-laravel)[ Packagist](https://packagist.org/packages/esyede/hcaptcha-laravel)[ RSS](/packages/esyede-hcaptcha-laravel/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (4)Used By (0)

Laravel hCaptcha
================

[](#laravel-hcaptcha)

Integrate hCaptcha into your Laravel application.

Installation
------------

[](#installation)

Install it with [Composer](https://getcomposer.org):

```
composer require esyede/hcaptcha-laravel
```

Publish the configuration file:

```
php artisan vendor:publish --provider="Esyede\Laravel\HCaptcha\HCaptchaServiceProvider"
```

Setup
-----

[](#setup)

Register and add your website to the [hCaptcha dashboard](https://dashboard.hcaptcha.com) to obtain site key and secret key.

Then add that to your `.env` file:

```
HCAPTCHA_SITEKEY=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
HCAPTCHA_SECRET=0x0000000000000000000000000000000000000000
```

Usage
-----

[](#usage)

### Display

[](#display)

To display the widget:

```
{!! HCaptcha::display() !!}
```

You can also set [custom attributes](https://docs.hcaptcha.com/configuration#hcaptcha-container-configuration) on the widget:

```
{!! HCaptcha::display(['data-theme' => 'dark']) !!}
```

Or customize the CSS class:

```
{!! HCaptcha::display([
    'class' => $errors->has('email') ? 'is-invalid' : '',
]) !!}
```

### Script

[](#script)

To load the hCaptcha javascript resource:

```
{!! HCaptcha::script() !!}
```

You can also set the [query parameters](https://docs.hcaptcha.com/configuration):

```
{!! HCaptcha::script($locale, $render, $onload, $recaptchacompat) !!}
```

### Validation

[](#validation)

To validate the hCaptcha response, use the `hcaptcha` rule:

```
$request->validate([
    'h-captcha-response' => ['hcaptcha'],
]);
```

*You can leave out the `required` rule, because it is already checked internally.*

#### Custom validation message

[](#custom-validation-message)

Add the following values to your `validation.php` in the language folder:

```
'custom' => [
    'h-captcha-response' => [
        'hcaptcha' => 'Please verify that you are human.',
    ]
],
```

### Invisible Captcha

[](#invisible-captcha)

You can also use an [invisible captcha](https://docs.hcaptcha.com/invisible) where the user will only be presented with a hCaptcha challenge if that user meets challenge criteria.

The easiest way is to bind a button to hCaptcha:

```
{!! HCaptcha::displayButton() !!}
```

This will generate a button with an `h-captcha` class and the site key. But you still need a callback for the button:

```

    function onSubmit(token) {
        document.getElementById('my-form').submit();
    }

```

By default, `onSubmit` is specified as callback, but you can easily change this (along with the text of the button):

```
{!! HCaptcha::displayButton('Submit', ['data-callback' => 'myCustomCallback']) !!}
```

You can also set other [custom attributes](https://docs.hcaptcha.com/configuration#hcaptcha-container-configuration), including `class`.

Usage without Laravel
---------------------

[](#usage-without-laravel)

This package can also be used without Laravel. Here is an example of how it works:

```
