Contact Us

Use reCAPTCHA in Laravel 8 Forms For Validation.

Mobile App | March 3, 2022

Forms are the main source of spam on any website. Whether it is the traditional contact forms or custom forms for user feedback, abusing the forms to (intentionally or unintentionally) overwhelm the server is a common attack on a website.

Google ReCaptcha V3 is a captcha-like framework that provides security against hackers and sticks or twist requests. It assures that the computer user is a human.

reCAPTCHA is an industry-standard for preventing spam by greatly enhancing form validation capabilities. It is a combination of knowledge and image recognition-based interpretive response based on answer selection. The methodology relies on machine learning from Google’s large data sets of human interaction with the web, which is slightly different and random when compared to a bot.

In this article, I will demonstrate how to use Google reCAPTCHA for form validation in Laravel 8.x

This one requires only two things:

Setting up reCAPTCHA in Laravel 8

Let’s get the necessary classes we will require to make things possible. I’ll be using Composer to bring down the packages:

Set up Configurations

We are going to edit the app.php file.

Insert these lines in “providers” array:

Now, let’s setup our secret keys. Go to this link, and enter your domain address, like I have shown in image below, to grab your “secret key” and “site key”.

Make changes to your “.env” file:

Insert these lines:

Set up Routes

These are our entry points of our app, let’s make some changes.

Paste the code below:

Save and exit. As you see, our routes are accessing a FileController which has two functions to get and post captcha form.

Make the Controller

Let’s make our controller.

Copy and paste the code below:

Save and exit.

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

Let’s make our UI which will make us use of the reCAPTCHA service.

And, paste this code below.

Let’s save and exit

Let’s Test the Form

Go to your application and see if you can find the reCAPTCHA.:

This article is a simple example of how you can secure your web application by adding reCAPTCHA. reCAPTCHA in Laravel is the leading and most used captcha framework, where clients are only required to tap on a checkbox and in a few cases select a few comparative pictures related to the conman address. This adds an extra layer of security, eluding spam and improving your form validation capability.

Share your opinion in the comment section.
COMMENT NOW

Share This Article

“Cloudways hosting has one of the best customer service and hosting speed”

Sanjit C [Website Developer]

Inshal is a Content Marketer at Cloudways. With background in computer science, skill of content and a whole lot of creativity, he helps business reach the sky and go beyond through content that speaks the language of their customers. Apart from work, you will see him mostly in some online games or on a football field.

This content was originally published here.