How to add reCaptcha to a form
Tags: laravel, beginners, spam
Published: 2019-03-11 15:13:06

---
title: How to add reCaptcha to a form
published: true
description: how to integrate Google's reCaptcha in a form
tags: laravel,beginner,spam
---
<p><em>This article was originally posted in <a href="http://www.antonioufano.com" target="_blank">my website</a>. If you like it, you may find interesting previous articles in <a href="http://www.antonioufano.com/blog" target="_blank">my blog</a></em><p>
<hr>
<p>One of the most annoying things of having a form in a website is seeing it being targeted by spam bots. In this article I'm going to explain how to get rid of them (or at least most of them...) by integrating Google's reCaptcha in our form.<p><em><b>Note: </b>For this example I'll be using a Laravel app but I'll explain the basic concepts of how reCaptcha works and how to integrate it so, if you're using a different framework, you should be able to follow along.</em></p> We'll just need to follow a few simple steps:</p><ul data-children-count="0"><li>Register your site in reCaptcha and get your site and secret keys.<br></li><li>Add the keys to the environment of our app (.env file in Laravel).<br></li><li>Include the reCaptcha input in our page form.</li><li>Add the reCaptcha validation in our back end (a controller method in Laravel).</li></ul>

<h3 data-children-count="0">Register your website</h3>
<p data-children-count="0">Registering your website to use reCaptcha is pretty straight forward. Just visit <a href="https://www.google.com/recaptcha/admin" title="recaptcha admin" target="_blank">this link</a>&nbsp;(logged in with your Google account) and fill the form. In the label, put something that will easily identify your site and in the reCaptcha type, I selected the v2 Checkbox (note that the integration is different depending on the type).</p><p data-children-count="0">Once registered you'll get a screen with your unique keys and the client/server integration details:</p><p style="text-align: center;" data-children-count="0"><img src="http://www.antonioufano.com/image_uploads/reCaptcha_keys.png" alt="keys" style="font-size: 1rem;"></p><p data-children-count="0">And that's all we have to do from the reCaptcha side. Now we just have to integrate it into our application.<br></p>

<h3>Add keys to the application environment</h3>
<p data-children-count="0">We'd need to use our client and server keys to call the reCaptcha API from both the front and back end of our application so in order to have them available in our whole app, we can add the as environment variables or, as my app is built in Laravel, I'll use the .env file adding them at the end of it:</p><pre data-children-count="0"><code class="language-php">// .env file
GOOGLE_RECAPTCHA_KEY=YOUR_RECAPTCHA_SITE_KEY
GOOGLE_RECAPTCHA_SECRET=YOUR_RECAPTCHA_SECRET_KEY
</code></pre>
In JavaScript you can load them as environment variables and access them with <em><strong>process.env.VARIABLE_NAME</em></strong>.
<br>

<h3 data-children-count="0">Add the reCaptcha input in the form</h3>
<p data-children-count="0">Next step is to include the reCaptcha checkbox in our form using the two code snippets from the reCaptcha dashboard. For this example I have a contact form with a few inputs (name, email and text) that sends all the payload via a POST request to a route called "contact.send" which is handled by a controller. It looks like this:</p>
{% gist https://gist.github.com/uF4No/4a3a2bc2b1b123c7cf06de6aa5e8d093 %}

<p>The scripts provided in the reCaptcha dashboard contain the input that we'll have to add to our form, and the script that is executed when it's triggered. Here's how our previous form looks after I've added the snippets:</p>
{% gist https://gist.github.com/uF4No/a9bd874396214ad545e2de10d30c8d43 %}

<p>Note that the input snippet provided includes our site key hardcoded into it and, as I've included it in the .env file, I've replaced it with a reference to the variable ;) At this point we should be able to see the reCaptcha input when we load our form, but there is an additional step we need to do before it actually works.</p>

<h3 data-children-count="0">Add reCaptcha validation in the back end</h3>
<p data-children-count="0">In order to understand the changes that we need to do in our controller, we first need to understand what actually happens when a user clicks on the reCaptcha input.</p><ul data-children-count="0"><li>A request is sent to the Google servers in order to do the verification. This requests contains our site key so the verification is linked to our site.</li><li>Google server responds with&nbsp; a unique id, which is included in our form in an input with the name "g-recaptcha-response". Once the form is submitted, we'll be able to access the "g-recaptcha-response" to obtain the verification id.<br></li></ul><p style="text-align: left;" data-children-count="0">As part of the server side normal validations in our controller (check that all fields of our form are filled and with the correct format etc...) we'll need to also check if Google has successfully verified that our user is not a bot by sending a POST request to Google's /recaptcha/api/siteverify including our secret key and the verification id from the "g-recaptcha-response" input, as indicated in the reCaptcha documentation:</p><p style="text-align: center;" data-children-count="0"><img src="http://www.antonioufano.com/image_uploads/reCaptcha_server.png" style="font-size: 1rem;" alt="reCaptcha server integration"></p><p style="text-align: left;" data-children-count="0">In PHP we can do this using the <strong style="font-style: italic;">file_get_contents()</strong> function to send the POST request, and the <strong><em>json_decode()</em></strong> function to parse the response to a JSON object as follows:</p>{% gist https://gist.github.com/uF4No/c44dbf39c118f7482eddd4fa7459d615 %}<p style="text-align: left;" data-children-count="0">In JavaScript, you can use <a  href="https://www.npmjs.com/package/axios" target="_blank">axios</a> to send the POST request and then <strong><em>JSON.parse()</em></strong> to transform the response to an object.</p><p> The response from our POST request will contain a <em style="">"success"</em> property with a true/false value which we'll use to return an error message or, as in this example, store the message in our database.</p>

<h3 style="text-align: left;" data-children-count="0">Conclusion</h3>
<p data-children-count="0">For me, adding reCaptcha to the public forms of the websites has become a must if you dont want to have to deal with tons of spam and bots. This article only covers the reCaptcha v2 checkbox, but different types can be integrated in a similar manner. Hope you find this article helpful :)</p><p data-children-count="0">Happy coding!&nbsp;</p>