Laravel 6: Google Recaptcha Code With Validation

What is Google Recaptcha ?

reCAPTCHA is a free service that protects your website from spam and abuse. reCAPTCHA uses an advanced risk analysis engine and adaptive challenges to keep automated software from engaging in abusive activities on your site. It does this while letting your valid users pass through with ease.

Google ReCaptcha is a captcha like system, that provide security against hackers and sticks or curl requests. It assures that a computer user is a human. It is the best and most used captcha system available where users are only required to click on a checkbox and in some cases select some similar images related to conman question.

Also Read: Captcha Code and Validation Example using Botdetect Package in Laravel

In this article, we will implement Recaptcha in forms in Laravel 5.8 i write step by step tutorial of how to use google captcha using 'anhskohbo/no-captcha' package in laravel 5.8 application. you can simple use google re-captcha code in your registration form too.

In this example, we will create simple registration form and implement google captcha code. before use google captcha code we will install "anhskohbo/no-captcha" composer package for google captcha. You have to just follow few step and you will get google re-captcha code in your laravel 5.8 app.

Step 1 : Download Laravel 5.7

first of all we need to get fresh Laravel 5.7 version application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Install anhskohbo/no-captcha Package

In this step we need to install anhskohbo/no-captcha via the Composer package manager, so one your terminal and fire bellow command

composer require anhskohbo/no-captcha

After successfully install package, we require to add aliases and service provider.

Config/app.php

 [
         Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class
     ],

     'aliases' => [
         'NoCaptcha' => Anhskohbo\NoCaptcha\Facades\NoCaptcha::class,
     ]
  ]

Step 3: Update Google API Key

In this step we need to set google site key and secret key. If you don't have site key and secret key then you can create from here. First click on this link : Recaptcha Admin

After click you can see bellow view and you need register your site link this way:

recaptcha-code-with-validation

Now open .env file and add this two variable

.env

NOCAPTCHA_SITEKEY=[site-key]
NOCAPTCHA_SECRET=[secret-key]

Step 4: Add Route

We will use Laravel default auth mechanism to complete our project. So we have no need to create new controller. So go to your app/Http/Controllers/Auth/RegisterController.php. In your register controller your default validator function will be look like this. 

 protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'slug' => 'required',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|min:6|dumbpwd|confirmed'
        ]);
    }

Now you have to just add one line code. see the below code

 protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'slug' => 'required',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|min:6|dumbpwd|confirmed',
            'g-recaptcha-response' => 'required|captcha',
        ]);
    }

Look we just added 'g-recaptcha-response' => 'required|captcha' line to get Capctcha code. 

Step 5: Create View File

This is the last step. So go to your resources/views/auth/register.blade.php and add the following code to your register form.

Finally, Our How to Generate Captcha Code in Laravel is over. Hope it will work for you

Codechief is a very fast growing community among programmers and have a reach of around 1 million+ readers globally. Contribution at Codechief is open for all those who have a passion to learn and help others by sharing their knowledge. If you think you have the zeal to learn, start contributing on Codechief contribute. you can also mail your article to [email protected] See your article appearing on the codechief main page and help other code.

We believe that everyone has the right to learn, so we allow both students and professionals to contribute on Codechief.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website