How to Use reCAPTCHA in Laravel 5.2 Forms for Validation

by Noor Ali  August 4, 2016

In the year 1950, the father of modern computer sciences, Alan Turing developed a behavioral test that was to distinguish the machine-based behavior from the humans. The test became known as Turing’s Test, and the successful machines to complete this Turing test were considered equally good as humans in those particular tasks.

Turing planted the seed which has now grown into a completely new branch of computer science known as Artificial Intelligence. Since then, the bots have gotten smarter and sharper, which led us to the difficulty of sorting out the organic and bot traffic. These could be used for any false advertising impression or spamming at some web service. To overcome these problems, developers around the world came up with ideas to keep the bots at bay.

One of the ways to filter bot traffic is by using reCAPTCHA. It is a reverse Turing Test method to draw a line between organic and bots. reCAPTCHA, a rework of once-popular CAPTCHA, is a free online tool acquired and provided by Google.

It is a combination of knowledge and recognition-based interpretive response based on answer selection. The methodology lies in the machine learning from the Google’s large data sets of human interaction with the web, which is slightly different and random compared to a bot.

So, let’s learn how you can use the Google reCAPTCHA in Laravel to validate your form.


This one requires only two things:

  • You must have some knowledge about Laravel 5.2.
  • You must have a launched Laravel app on Cloudways PHP Platform. This one’s optional.

Step 1: Getting Necessary Packages

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

Step 2: Setting up Configurations

We are going to edit the app.php file.

Insert these lines in “providers” array:

Insert these lines in “aliases” array:

Step 3: Setting up Secret Keys

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”.

Registering for reCAPTCHA

Make changes to your “.env” file:

Insert these lines:

Step 4: Setting 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.

Step 5: Making the Controller

Let’s make our controller.

Copy and paste the code below:

Save and exit.

Step 6: Making the Views

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

And, paste this code below.

Let’s save and exit

 Step 7: Let’s Test the Form

Go to your app link and let’s see if you can find the reCAPTCHA. Something like this image below:

Initial view of the form with reCAPTCHA

When you will check “I’m not a robot” box, you will see a new popup with a puzzle.

reCAPTCHA in action

That is it, guys! Have a good coding week!

Create Laravel apps without the worry of server management.

Deploy your app on optimized PHP hosting servers for Laravel.

About Noor Ali

Noor Ali is an Associate Software Engineer at Cloudways. He loves to solve technical problems through programming and mathematics.

Stay Connected:

You Might Also Like...

  • Jose

    Does this work with multiple forms in the same page?