PHPackages                             teampanfu/laravel-hcaptcha - 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. teampanfu/laravel-hcaptcha

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

teampanfu/laravel-hcaptcha
==========================

A Laravel package to display and validate hCaptcha.

v1.1.2(3y ago)1596.2k↓48.3%2MITPHPPHP ^7.2|^8.0

Since May 14Pushed 10mo agoCompare

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

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

hCaptcha package for Laravel
============================

[](#hcaptcha-package-for-laravel)

[![Visit hCaptcha.com](https://user-images.githubusercontent.com/59781900/163660320-8209d05d-c7ed-40f3-831b-3dde16904014.png)](https://www.hcaptcha.com/)

[![Latest Version](https://camo.githubusercontent.com/2472334ae11bedc2839a381b2a26d19d80af5dc688c2d7af18b757e34b589360/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f7465616d70616e66752f6c61726176656c2d68636170746368612e7376673f7374796c653d666c61742d737175617265)](https://github.com/teampanfu/laravel-hcaptcha/releases)[![Total Downloads](https://camo.githubusercontent.com/338e73bc6b223fce157d9c4934870404b0b4be1241fa52f1b56a97345965f562/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7465616d70616e66752f6c61726176656c2d68636170746368612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/teampanfu/laravel-hcaptcha)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)

A package specifically designed to include [hCaptcha](https://www.hcaptcha.com) directly into [Laravel](https://laravel.com).

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

[](#installation)

To install, use [Composer](https://getcomposer.org):

```
composer require teampanfu/laravel-hcaptcha
```

Manual setup
------------

[](#manual-setup)

As of Laravel 5.5, packages are discovered automatically via [package discovery](https://laravel.com/docs/9.x/packages#package-discovery). So if you are using a newer version, you can skip these steps.

Add the following to your `config/app.php`:

```
'providers' => [
    ...

    /*
     * Package Service Providers...
     */
    Panfu\Laravel\HCaptcha\HCaptchaServiceProvider::class,

    ...
],

'aliases' => [
    ...
    'HCaptcha' => Panfu\Laravel\HCaptcha\Facades\HCaptcha::class,
    ...
],
```

Then publish the configuration file:

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

Configuration
-------------

[](#configuration)

Add your website in the [hCaptcha dashboard](https://dashboard.hcaptcha.com) to get a site key and secret key.

When you have done that, add the keys to your `.env` file:

```
HCAPTCHA_SITEKEY=10000000-ffff-ffff-ffff-000000000001
HCAPTCHA_SECRET=0x0000000000000000000000000000000000000000
```

*These are the test keys we use by default. You should not use them in production!*

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 extend the 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' => ['required', 'hcaptcha'],
]);
```

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

Use without Laravel
-------------------

[](#use-without-laravel)

The package is designed so that it can be used without Laravel. Here is an example of how it works:

```
