• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Tutsplanet

Tutsplanet

Free Technical and Blogging Resources

  • Home
  • Web Hosting
  • Programming
  • Plugins
  • Twitter Trends
  • Tools
  • About Us

Programming TagsLaravel

How to Use reCAPTCHA in Laravel 5 Forms For Validation

Jul 14, 2018 Editorial Staff Leave a Comment

Share
Tweet
Share

There are plenty of options to prevent spam, but captcha is one of the best to use. When talking about captcha, the first thing will be in mind is weird characters seen on the online forms. Here we talk about Google’s Recapthca integration to your Laravel application.

First thing is first, let’s go to https://www.google.com/recaptcha website and register our new site there. If you are redirecting to a intro page, just click on the my captcha’s link on the top. There you will see a form with title, Register a new site. Create your new site and generate secret keys.

Google reCaptcha in Laravel application

I’ve added the captcha key into the .env file

GCAPTCHA_SECRET=6Lf5RlwUAAAXXXXXXXXXXXXXXXXXXXXXXXXXX

I want this widget to appear after password confirmation filed, before Register button.

<div class="form-group row mb-0">
    <div class="col-md-6 offset-md-4">
        <div class="g-recaptcha" data-sitekey="{{ env('GCAPTCHA_SECRET') }}"></div>
        
    </div>
</div>
<div class="form-group row mb-0">
    <div class="col-md-6 offset-md-4">
        <button type="submit" class="btn btn-primary">
            {{ __('Register') }}
        </button>
    </div>
</div>

See how it’s appearing in the Laravel register form.

Google reCaptcha in Laravel application

Backend Integration of Google Recaptcha

We are not using, any Captcha packages for Laravel, we build it custom. So let’s create custom validator to validate the Google Captcha. Laravel has the option to create custom validators in your own way.

Create a file called Recapthca.php in the folder app/Validators.  You can read more about Laravel custom validators here. I have used https://github.com/guzzle/guzzle to deal HTTP client operations.  This custom validator will do all the needful validations for the Google Captcha.

<?php
namespace App\Validators;
use GuzzleHttp\Client;
class Recaptcha {
    public function validate(
        $attribute,
        $value,
        $parameters,
        $validator
    ){
        $client = new Client();
        $response = $client->post(
            'https://www.google.com/recaptcha/api/siteverify',
            ['form_params'=>
                [
                    'secret'=>env('GCAPTCHA_SECRET'),
                    'response'=>$value
                ]
            ]
        );
        $body = json_decode((string)$response->getBody());
        return $body->success;
    }
}

Let’s start with validation in the controller.

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

And the final step will be to register our new validation in AppServiceProvider.php in file app/Providers/AppServiceProvider.php

Import the facade validate

use Illuminate\Support\Facades\Validator;
public function boot()
{      
   Validator::extend(
          'recaptcha',
          'App\\Validators\\[email protected]'
   );
}

Hope you’ve had enjoyed other Laravel articles too.


Editorial Staff

Editorial Staff at Tutsplanet is a dedicated team to write various tutorials about subjects like Programming, Technology and Operating Systems.

View all posts by Editorial Staff

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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

Primary Sidebar

Quick Links

  • Top 21 Website Ideas To Make Money Online in 2021
  • A Simple YouTube Video Downloader Script in PHP
  • The 50 Most Useful jQuery Plugins for Frontend Development
  • Replace “\n” with new line characters, using Notepad++
  • Using Third-Party Libraries in Codeigniter
  • Upload Multiple Images and Store in Database using PHP and MySQL.
  • Hierarchical Tree view Category Example in Laravel
  • Laravel Image Intervention Tutorial With Example
  • How to import sql file in MySQL database using PHP?
  • Free VAT Calculator Online

Subscribe

* indicates required



Search Here

Share

   



Hot topics

  • Replace “\n” with new line characters, using Notepad++ 14 views
  • Solution: windows photo viewer opens each time save a pic file 12 views
  • Simple PHP Shopping Cart 10 views
  • A Simple YouTube Video Downloader Script in PHP 10 views
  • How to Create PDF File From HTML Form Using Fpdf? 9 views
  • All Countries SQL file with Country Codes to Download 8 views
  • How to create and download a Zip file with PHP 8 views
  • How to set up GitHub SSH in Ubuntu with example 7 views
  • How to enter new line in Microsoft Teams? 7 views
  • Hierarchical Tree view Category Example in Laravel 7 views

Categories

  • Design & Development
  • Drupal
  • Facebook
  • General
  • How To
  • ios
  • Javascript
  • Linux
  • Magento
  • Marketing
  • News
  • PHP
  • Plugins
  • Programming
  • Snippets List
  • Social Media
  • Softwares
  • Themes
  • Tips
  • Wordpress
  • YouTube























Copyright © 2023 · Planet on Genesis Framework · Powered By BunnyCDN . Network wallpapernoon.com