Google reCAPTCHA with Contact Form

Google reCAPTCHA with Contact Form
Google reCAPTCHA is CAPTCHA like system which is completely free service and protect your website from spam bots submission. This service designed to validate that user is a human, just need one click to confirm it.

Google reCAPTCHA is more than just a spam protector. Each time user solve the CAPTCHA, it help to understand annotate image and digitize text.

In this post we will explain you how to use Google reCAPTCHA in HTML form. You can easily integrate this in any PHP script. Lets start how we can do this:

Create Google reCAPTCHA Contact Form

As we told you Google reCAPTCHA is a Free service. First you need to register you website here: https://www.google.com/recaptcha/admin

register for google recaptcha

Choose the type of reCAPTCHA as per your requirement. We are creating contact form for a website, so choose reCaptcha V2 option to validate the request.

After this registration you will get Site Key and Secret Key. Google also provide reCAPTCHA code library for client side integration.

google recaptcha keys

include this library in your HTML page before closing the </head> tag.

<script src=’https://www.google.com/recaptcha/api.js’></script>

Now create an HTML contact form and paste this snipped in the end of form to show Google reCapcha.

<div class=”g-recaptcha” data-sitekey=”6LfQMl0UA*************BrwXXZwp”></div>

See below HTML form complete code example. Must update the data-sitekey attribute value with your Site Key in HTML form.

On form submit, you will get the reCAPTCHA response token with the name of string ‘g-recaptcha-response’. To validate the CAPTCHA, send a request to this URL with below parameters.

Request URL: https://www.google.com/recaptcha/api/siteverify

secret (required) : 6LfQMl0UA*************DvLuCPLvWhOIY8
response (required) : The value of ‘g-recaptcha-response’

After validating this response token, the post request return JSON response. Check the response is Success or not. On Success response perform contact form action part and if it is false to validate than show CAPTCHA verification failed message.

Check the Form Action PHP file code here:

Using this way you can easily implement Google reCAPTCHA with contact form. This will protect your website from spam and auto fill submissions.

View the Live Demo and Download the Free Source code

DEMO

Leave a Comment