Integrating reCAPTCHA with PHP

reCAPTCHA is a free service by Google that protects your site from spam. In this tutorial, I will walk you through integrating reCAPTCHA in PHP.

Davison Pro
3 min readSep 29, 2019

--

reCAPTCHA is a free service by Google that protects your site from spam and other types of automated abuse. It uses advanced risk analysis techniques to tell humans and bots apart.

This tutorial is aimed at people who are familiar with basic PHP and HTML forms. The source code of this tutorial is available here.

Sign up for your API keys reCAPTCHA API keys

To start using reCAPTCHA you will need to register your website to get the reCAPTCHA API key pair from Google. The key pair consists of a site key and a secret key. The site key is used to call or invoke the reCAPTCHA service on your site. The secret key authorizes communication between your application backend and the reCAPTCHA server to verify the user’s response. The secret key needs to be kept safe for security purposes.

Fill in the form and select the reCAPTCHA v2 and in that select “I am not a robot” checkbox option.

Once submitted, Google will provide you with your site key and secret key.

After you’ve signed up for your API keys, below are basic instructions for installing reCAPTCHA on your site. We will be using a simple form as an example.

Adding reCAPTCHA widget

To add reCAPTCHA to your site you will first need to add the reCAPTCHA JavaScript library in your HTML.

<script src='https://www.google.com/recaptcha/api.js' async defer ></script>

Now we will add the below HTML code to our form where we want the reCAPTCHA widget to appear.

<div class="g-recaptcha" data-sitekey="site_key"></div>

--

--

Davison Pro

JavaScript enthusiast, Full Stack Web Developer and blogger at https://davisonpro.dev