PHPackages                             anam/captcha - 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. anam/captcha

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

anam/captcha
============

reCAPTCHA and invisible reCAPTCHA for Laravel. reCAPTCHA protects your app against spam and bot.

v1.0.1(6y ago)2843.4k↓41.1%6[3 issues](https://github.com/anam-hossain/captcha/issues)[1 PRs](https://github.com/anam-hossain/captcha/pulls)MITPHPPHP &gt;=7.0.0

Since Oct 22Pushed 6y ago2 watchersCompare

[ Source](https://github.com/anam-hossain/captcha)[ Packagist](https://packagist.org/packages/anam/captcha)[ Docs](https://github.com/anam-hossain/captcha)[ RSS](/packages/anam-captcha/feed)WikiDiscussions master Synced 1mo ago

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

reCAPTCHA V2 and invisible reCAPTCHA for Laravel
================================================

[](#recaptcha-v2-and-invisible-recaptcha-for-laravel)

reCAPTCHA protects your app against spam and bot. This package is tested with Laravel 5.5.

[![recaptcha](https://camo.githubusercontent.com/edea3ac49b0f8ecab9889986fb29c4c49b38740b899b39d2d32e61c5b9ec5cee/68747470733a2f2f646576656c6f706572732e676f6f676c652e636f6d2f7265636170746368612f696d616765732f6e657743617074636861416e63686f722e676966 "reCAPTCHA V2")](https://camo.githubusercontent.com/edea3ac49b0f8ecab9889986fb29c4c49b38740b899b39d2d32e61c5b9ec5cee/68747470733a2f2f646576656c6f706572732e676f6f676c652e636f6d2f7265636170746368612f696d616765732f6e657743617074636861416e63686f722e676966)

Requirements
------------

[](#requirements)

- PHP 7.0+

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

[](#installation)

`Captcha` is available via Composer:

```
$ composer require anam/captcha
```

Alternatively, add the dependency directly to your composer.json file:

```
"require": {
    "anam/captcha": "~1.0"
}
```

Integrations
------------

[](#integrations)

#### Laravel 5.5+ integrations

[](#laravel-55-integrations)

##### Package Discovery

[](#package-discovery)

`Anam\Captcha` utilize the Laravel's package auto discovery feature. So, you don't need to add manually Service provider and Facade in Laravel application's config/app.php. Laravel will automatically register the service provider and facades for you.

#### Laravel &lt; 5.5 integrations

[](#laravel--55-integrations)

Captcha comes with a Service provider and Facade for easy integration.

After you have installed the `anam/captcha`, open the `config/app.php` file which is included with Laravel and add the following lines.

In the `$providers` array add the following service provider.

```
'Anam\Captcha\ServiceProvider\CaptchaServiceProvider'
```

Add the facade of this package to the `$aliases` array.

```
'Captcha' => 'Anam\Captcha\Facade\Captcha'
```

You can now use this facade in place of instantiating the converter yourself in the following examples.

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

[](#configuration)

First, register keys for your site at

Add `RECAPTCHA_SITE_KEY` and `RECAPTCHA_SECRET` in `.env` file :

```
RECAPTCHA_SITE_KEY=site_key
RECAPTCHA_SECRET=secret

```

Run vendor publish to add the captcha.php file to config:

```
php artisan vendor:publish --tag=CaptchaConfig

```

By default, The package will try to load keys from environment. However, you can set them manually:

```
$captcha = new \Anam\Captcha\Captcha('recaptcha_secret');
```

Blade directives:

```
// reCAPTCHA v2
@captcha(site_key)

// Invisible reCAPTCHA
@invisiblecaptcha(site_key)
```

Usage
-----

[](#usage)

### Client side

[](#client-side)

#### reCAPTCHA V2:

[](#recaptcha-v2)

Just add `@captcha()` blade directive to the form.

```

  {{ csrf_field() }}
  Name

  Your message

  @captcha()

```

For more advanced integration, Please visit the following link:

#### Invisible reCAPTCHA:

[](#invisible-recaptcha)

Add `@invisiblecaptcha()` directive to the form where you want to appear the submit button. Please note, The `@invisiblecaptcha` directive will inject the submit button for you. If you want to style the submit button, `.g-recaptcha` class available for you.

```

  {{ csrf_field() }}
  Name

  Your message

  @invisiblecaptcha()

```

Caveat: If view has more than one forms, the `@invisiblecaptcha()` might not work as it will submit the first form. In these cases, you have to integrate the reCAPTCHA manually.

Please visit the following link:

### Server side

[](#server-side)

**Handling the request:**

```
use Anam\Captcha\Captcha;
use Illuminate\Http\Request;

class CaptchaController extends Controller
{
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Anam\Captcha\Captcha  $captcha
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Captcha $captcha)
    {
        $response = $captcha->check($request);

        if (! $response->isVerified()) {
            dd($response->errors());
        }

        dd($response->hostname());
    }
}
```

Example
-------

[](#example)

### Laravel User Registration Controller

[](#laravel-user-registration-controller)

**app\\Http\\Controllers\\Auth\\RegisterController.php**

```
