Laravel 8 google recaptcha form validation example tutorial
In this tutorial, we are going to integrate the Google reCAPTCHA in Laravel 8. normally captcha is used for security purposes and only human users can pass through. computers or bots are not solving a captcha. so we will learn about captcha that how to integrate the Google reCAPTCHA in laravel 8.
In this example, we will integrate the Google reCAPTCHA in laravel 8 using the “anhskohbo/no-captcha” pacakge. so you can follow the below step.
Overview
Step 1: Install Laravel
Step 2: Install Package
Step 3: Add providers and aliases
Step 4: Get and Set Google Captcha Secrets
Step 5: Create Route
Step 6: Create a Controller
Step 7: Create Blade Files
Step 8: Run The Application
Step 1: Install Laravel
We are going to install laravel 8, so first open the command prompt or terminal and go to xampp htdocs folder directory using the command prompt. after then run the below command.
1 | composer create-project --prefer-dist laravel/laravel laravel8_google_captcha |
Step 2: Install Package
Now, we are going to install the “anhskohbo/no-captcha” package using the below command.
1 | composer require anhskohbo/no-captcha |
Step 3: Add providers and aliases
We will add below providers and aliases in the “config/app.php” file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 'providers' => [ .... Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class ], 'aliases' => [ .... 'NoCaptcha' => Anhskohbo\NoCaptcha\Facades\NoCaptcha::class, ] |
Step 4: Get and Set Google Captcha Secrets
First, we need to Google reCAPTCHA site key and secret key. so first we go to Google reCAPTCHA panel and get the site key and secret key.
After getting the keys, we will set the key in the .env file. so you can see our code.
1 2 | NOCAPTCHA_SITEKEY=Enter_Site_Key NOCAPTCHA_SECRET=Enter_Secret_Key |
Step 5: Create Route
Add the following route code in the “routes/web.php” file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php use App\Http\Controllers\StudentController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('student',[StudentController::class, 'index']); Route::post('student',[StudentController::class, 'save'])->name('student.save'); ?> |
Step 6: Create a Controller
Here below command help to create the controller.
1 | php artisan make:controller StudentController |
StudentController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Validator,Redirect,Response,File; class StudentController extends Controller { /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function index() { return view('student'); } /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function save(Request $request) { request()->validate([ 'txtFirstName' => 'required', 'txtLastName' => 'required', 'txtEmail' => 'required', 'txtMobile' => 'required', 'g-recaptcha-response' => 'required|captcha', ]); dd('captcha code is valid'); } } ?> |
Step 7: Create Blade Files
So finally, first we will create the student.blade.php files in the “resources/views” directory.
student.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | <!DOCTYPE html> <html lang="en"> <head> <title>Laravel 8 Google Recaptcha Form Validation Example Tutorial - XpertPhp</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <form method="post" name="frmAdd" id="frmAdd" action="{{ route('student.save') }}"> @csrf <div class="form-group"> <label for="txtFname">First Name:</label> <input type="text" name="txtFname" class="form-control" id="txtFname" value=""> @if ($errors->has('txtFname')) <span class="error"> <p>{{ $errors->first('txtFname') }}</p> </span> @endif </div> <div class="form-group"> <label for="txtLname">Last Name:</label> <input type="text" name="txtLname" class="form-control" id="txtLname" value=""> @if ($errors->has('txtLname')) <span class="error"> <p>{{ $errors->first('txtLname') }}</p> </span> @endif </div> <div class="form-group"> <label for="txtEmail">Email:</label> <input type="text" name="txtEmail" class="form-control" id="txtEmail" value=""> @if ($errors->has('txtEmail')) <span class="error"> <p>{{ $errors->first('txtEmail') }}</p> </span> @endif </div> <div class="form-group"> <label for="txtMobile">Mobile:</label> <input type="text" name="txtMobile" class="form-control" id="txtMobile" value=""> @if ($errors->has('txtMobile')) <span class="error"> <p>{{ $errors->first('txtMobile') }}</p> </span> @endif </div> <div class="form-group> <label class="control-label">Captcha</label> {!! app('captcha')->display() !!} @if ($errors->has('g-recaptcha-response')) <span class="error"> <p>{{ $errors->first('g-recaptcha-response') }}</p> </span> @endif </div> <button type="submit" class="btn btn-default">Submit</button> </form> </div> </body> </html> |
Step 8: Run The Application
We can start the server and run this application using the below command.
1 | php artisan serve |
Now we will run our example using the below Url in the browser.
1 | http://127.0.0.1:8000/student |