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

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

zbrettonye/no-captcha
=====================

No CAPTCHA reCAPTCHA For Laravel6~8

v1.3.0(2y ago)05.5kMITPHPPHP &gt;=7.2

Since Mar 20Pushed 2y agoCompare

[ Source](https://github.com/ZBrettonYe/reCAPTCHA)[ Packagist](https://packagist.org/packages/zbrettonye/no-captcha)[ RSS](/packages/zbrettonye-no-captcha/feed)WikiDiscussions main Synced today

READMEChangelog (6)Dependencies (3)Versions (11)Used By (0)

> 采用了 anhskohbo 的 package，在此基础上添加后台获取Google secret、sitekey 方法，可不从env获取。 更改 Google 源为 [www.recaptcha.net](http://www.recaptcha.net), 可通过GFW的封闭。
>
> 在此特别感谢 anhskohbo；

No CAPTCHA reCAPTCHA
====================

[](#no-captcha-recaptcha)

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

[](#installation)

```
composer require zbrettonye/no-captcha

```

Laravel 5 and above
-------------------

[](#laravel-5-and-above)

### Setup

[](#setup)

***NOTE*** This package supports the auto-discovery feature of Laravel 5.5 and above, So skip these `Setup` instructions if you're using Laravel 5.5 and above.

In `app/config/app.php` add the following :

1- The ServiceProvider to the providers array :

```
ZBrettonYe\NoCaptcha\NoCaptchaServiceProvider::class,
```

2- The class alias to the aliases array :

```
'NoCaptcha' => ZBrettonYe\NoCaptcha\Facades\NoCaptcha::class,
```

3- Publish the config file

```
php artisan vendor:publish --provider="ZBrettonYe\NoCaptcha\NoCaptchaServiceProvider"

```

### Configuration

[](#configuration)

#### Add the following to **.env** file

[](#add-the-following-to-env-file)

keyRequiredTypenotenoteNOCAPTCHA\_SECRETYstringSecretNOCAPTCHA\_SITEKEYYstringSite KeyNOCAPTCHA\_CONFIGNboolfalseMethod to get configNOCAPTCHA\_OPTIONSNarray\['timeout' =&gt; 30\]HTTP OptionsNOCAPTCHA\_SCORENboolfalseV3 feature, enable incorporate the score as a verification factor.NOCAPTCHA\_THRESHOLDNfloat0.7once NOCAPTCHA\_SCORE is enabled. Any requests above this score will be considered as spam```
NOCAPTCHA_SECRET=secret-key
NOCAPTCHA_SITEKEY=site-key

```

(You can obtain them from [here](https://www.google.com/recaptcha/admin))

### Usage

[](#usage)

#### Init js source

[](#init-js-source)

With default options :

```
 {!! NoCaptcha::renderJs() !!}
```

With [language support](https://developers.google.com/recaptcha/docs/language) or [onloadCallback](https://developers.google.com/recaptcha/docs/display#explicit_render) option :

```
 {!! NoCaptcha::renderJs('fr', true, 'recaptchaCallback') !!}
```

#### Display reCAPTCHA

[](#display-recaptcha)

Default widget :

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

With [custom attributes](https://developers.google.com/recaptcha/docs/display#render_param) (theme, size, callback ...) :

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

Invisible reCAPTCHA using a [submit button](https://developers.google.com/recaptcha/docs/invisible):

```
{!! NoCaptcha::displaySubmit('my-form-id', 'submit now!', ['data-theme' => 'dark']) !!}
```

Notice that the id of the form is required in this method to let the autogenerated callback submit the form on a successful captcha verification.

#### Validation

[](#validation)

Add `'g-recaptcha-response' => 'required|captcha'` to rules array :

```
$validate = Validator::make(Input::all(), [
	'g-recaptcha-response' => 'required|captcha'
]);
```

##### Custom Validation Message

[](#custom-validation-message)

Add the following values to the `custom` array in the `validation` language file :

```
'custom' => [
    'g-recaptcha-response' => [
        'required' => 'Please verify that you are not a robot.',
        'captcha' => 'Captcha error! try again later or contact site admin.',
    ],
],
```

Then check for captcha errors in the `Form` :

```
@if ($errors->has('g-recaptcha-response'))

        {{ $errors->first('g-recaptcha-response') }}

@endif
```

### Testing

[](#testing)

When using the [Laravel Testing functionality](http://laravel.com/docs/5.5/testing), you will need to mock out the response for the captcha form element.

So for any form tests involving the captcha, you can do this by mocking the facade behavior:

```
// prevent validation error on captcha
NoCaptcha::shouldReceive('verifyResponse')
    ->once()
    ->andReturn(true);

// provide hidden input for your 'required' validation
NoCaptcha::shouldReceive('display')
    ->zeroOrMoreTimes()
    ->andReturn('');
```

You can then test the remainder of your form as normal.

When using HTTP tests you can add the `g-recaptcha-response` to the request body for the 'required' validation:

```
// prevent validation error on captcha
NoCaptcha::shouldReceive('verifyResponse')
    ->once()
    ->andReturn(true);

// POST request, with request body including g-recaptcha-response
$response = $this->json('POST', '/register', [
    'g-recaptcha-response' => '1',
    'name' => 'John',
    'email' => 'john@example.com',
    'password' => '123456',
    'password_confirmation' => '123456',
]);
```

Without Laravel
---------------

[](#without-laravel)

Checkout example below:

```
