AppDividend
Latest Code Tutorials

Laravel Google reCaptcha Tutorial

2,480

Get real time updates directly on you device, subscribe now.

Laravel Google reCaptcha Tutorial is the topic, we will discuss today. The reCaptcha is an open service that defends your site from spam and abuse. It uses advanced risk analysis techniques to show humans and bots apart. Using reCaptcha, you must sign up for the API key pair for your website. The key pair consists of the site key and secret key. In this example, we can use anhskohbo/no-captcha package for creating reCaptcha.

Laravel Google reCaptcha Tutorial

First, we install Laravel Application.

Step 1: Configure Laravel Application

Install the brand new Laravel Application by the typing following command.

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

Step 2: Install anhskohbo/no-captcha Package

First, install anhskohbo/no-captcha Package for Google reCaptcha code. Using this Package, we can generate reCaptcha code for our Application. Switch to your command line and hit below command.

composer require anhskohbo/no-captcha

Step 3: Publish Config File

This package supports the auto-discovery feature of Laravel 5.5+.

Type below command to Publish Config File

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

Step 4: Set Google Site Key and Secret Key

Next, we need to set Google Site Key and Secret Key in .env file.  If you don’t have Site Key and Secret Key, then you have produce from below link.

Generate Recaptcha

reCaptcha Tutorial in Laravel

Now open the .env file and add this two keys.

NOCAPTCHA_SECRET=xxxx
NOCAPTCHA_SITEKEY=xxxx

Step 4: Create one controller

php artisan make:controller RecaptchaController --resource

It will create one controller file called RecaptchaController.php file.

Define validation in controller file.

//RecaptchaController.php

public function create()
    {
        return view('recaptchacreate');
    }
   
public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required|min:6',
            'g-recaptcha-response' => 'required|captcha'
        ]);

        return "success";
    }

In store() function, we have put the validation. If any validation fails it throws an error.

Step 5: Define Routes 

We list route in routes  >>  web.php file. So let us do it.

//web.php

Route::get('recaptchacreate', 'RecaptchaController@create');
Route::post('store', 'RecaptchaController@store');

In the web.php file, we define Two(2) routes the first route for display the login page, the second route for post request.

Step 6: Create one view file

Create a file in the resources  >>  views  >>   recaptchacreate.blade.php and set the following code in it.

//recaptchacreate.blade.php

<html lang="en">
<head>
    <title>reCAPTCHA Code in Laravel</title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">   
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>  
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
</head>
<body>
<div class="container">
      <h2>reCAPTCHA Code in Laravel</h2><br/>
      @if ($errors->any())
      <div class="alert alert-danger">
          <ul>
              @foreach ($errors->all() as $error)
                  <li>{{ $error }}</li>
              @endforeach
          </ul>
      </div><br />
      @endif
      <form method="post" action="{{url('store')}}">
        @csrf
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <label for="Name">Name:</label>
            <input type="text" class="form-control" name="name">
          </div>
        </div>
        <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-4">
              <label for="Email">Email:</label>
              <input type="text" class="form-control" name="email">
            </div>
          </div>
        <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-4">
              <label for="Password">Password:</label>
              <input type="password" class="form-control" name="password">
            </div>
          </div>
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
          <label for="ReCaptcha">Recaptcha:</label>
          {!! NoCaptcha::renderJs() !!}
          {!! NoCaptcha::display() !!}
            </div>
        </div>
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <button type="submit" class="btn btn-success">Submit</button>
          </div>
        </div>
      </form>
    </div>
</body>
</script>
</html>

 

Google reCaptcha Tutorial

Next, Start Laravel Development server by hit following command.

php artisan serve

If we do not enter any fields or enter invalid reCaptcha code, then we will get the errors.

Related Posts
1 of 39

Laravel 5.6 Google reCaptcha Tutorial

Finally, Our Laravel Google reCaptcha Tutorial is over. Thank You.

4 Comments
  1. Jitesh Lakhwani says

    There is small change in your RecaptchaController.php in store method you need to change the way you are using validator else it will trow an ssl error and there is also one extra tag at the last in the blade file.

    please use the below code for the store method:
    $validator = Validator::make($request->all(), [
    ‘name’ => ‘required’,
    ’email’ => ‘required|email’,
    ‘password’ => ‘required|min:6’,
    ‘g-recaptcha-response’ => ‘required|captcha’
    ]);

    i Googled a lot and invested my 45 min. then after changing this it started working properly.

    Thanks.

  2. shamim says

    Why Class ‘NoCaptcha’ not found sir in recaptchacreate page

  3. Ivan says

    There is an error :
    “Class ‘NoCaptcha’ not found”

  4. Rizki says

    can you make this tutorial to laravel 5.7 sir? i its not work when i try in my laravel 5.7 project. when i click checkbox “im not robot” chaptcha not display the puzzle. when i check console log its say failed load the recaptcha js, even though i have put {!! NoCaptcha::renderJs() !!} and {!! NoCaptcha::display() !!}on my form

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.