Laravel 8/7/6 Google ReCAPTCHA Form Validation Example
By Hardik Savani December 10, 2019 Category : LaravelPlayUnmuteLoaded: 1.15%FullscreenHi Dev,
Today, we will learn how to use google recaptcha in laravel 8/7/6. you can simply add google recaptcha in laravel 8/7/6. we will add laravel 7/6 google recaptcha code with form validation using anhskohbo/no-captcha composer package.
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.
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 6 app.
Step 1 : Download Laravel 6
first of all we need to get fresh Laravel 6 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.
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 bello view and you need register your site link this way:
Ok, after sucessfully register you can get site key and secret key from like bellow preview.
In this step, we have to create new controller as SiteAuthController and we have also need to add two methods siteRegister() and siteRegisterPost() on that controller like as you can see bellow:
app/Http/Controllers/SiteAuthController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class SiteAuthController extends Controller{ /** * Create a new controller instance. * * @return void */ public function siteRegister() { return view('siteRegister'); } /** * Create a new controller instance. * * @return void */ public function siteRegisterPost(Request $request) { $this->validate($request, [ 'name' => 'required', 'email' => 'required|email', 'password' => 'required|same:password_confirmation', 'password_confirmation' => 'required', 'g-recaptcha-response' => 'required|captcha', ]); print('done'); }}
Step 6: Create View File
In Last step, let's create siteRegister.blade.php(resources/views/siteRegister.blade.php) for layout and lists all items code here and put following code: