Setup And Validate reCaptcha With PHP

reCaptcha is a smart captcha system that will help prevent spammers from submitting junk data using forms on your website. The system works very well and is relatively simple to implement. This guide will show you how to setup and configure reCaptcha with PHP.

In order to set up reCaptcha, you will need to register for an account. You need to have an account because reCaptcha has an API that it uses to validate the answer given by the user. Most times the user will simply click a box and will be validated as a non-robot. Once you have registered you will have a public and secret key. Take note of these IDs as they are unique to your account.

recaptcha private and public keys
Clicking the cog will bring up the settings menu. In here you will be able to toggle the Recaptcha keys. The box will slide down to reveal the information. Make sure to keep the secret key secret.

Adding Recaptcha to your HTML

Start by including the following code in the <head> section of your page. This will include the javascript code required to make the reCaptcha work.

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

 The next thing you need to do is actually add the widget to your form. Go to the HTML <form> code for this post and add the following div inside the form tags.  

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

If you refresh your page you will now see the reCaptcha tool show up. That’s everything set up from the client-side of things. Next, you need to set up the code that will validate the captcha. This example shows you how to do it using PHP, but it’s simply a REST api. POST the answer to this API and you can get the answer back. This is simple code for any programming language.

Adding the PHP code to handle the response

The following code should be placed on the page that your form submits to. You can do this via AJAX if that’s how you do things. This form must take the variables passed from the form and it will allow you to verify whether the user has spoofed the response or not.

$captchaResponse = $_POST['g-recaptcha-response'];
 
$appsecret = ENTER-SECRET-HERE; 
$url = 'https://www.google.com/recaptcha/api/siteverify'; 
$data = array('secret' => $appsecret , 'response' => $captchaResponse); 

$options = array( 'http' => array( 
    'header' => "Content-type: application/x-www-form-urlencoded",
    'method' => 'POST', 
    'content' => http_build_query($data), ), ); 
$context = stream_context_create($options); 

$result = file_get_contents($url, false, $context); $result = json_decode($result, true); 
if($result["success"] == true) 
{ 
    //VALID!! 
}

Related Articles

Related Questions

WordPress Table of Contents Plus Not Working

I have been using this plugin for a while and i really like it. It seems to have completely stopped working recently. I can...

Function Keys Reversing Between Fn Actions And Normal

My keyboard has the usual F1 to F12 keys along the top. I use these for shortcuts in various applications. These keys also have...

Whirlpool Oven F6E6: Appliance Manager 1 Board Communication

I have a brand new Whirlpool oven W11I OM1 4MS2 H or (859991549450). I bought it alongside the microwave combi oven. I have had...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Tools

Markdown To Html Converter

Welcome to our web-based tool designed to make your life easier by converting Markdown to HTML in a matter of seconds. Our user-friendly interface...

AI Content Detector

We've got this awesome free tool that'll help you figure out if that content you're looking at was written by a human or some...

Image Saturation

Are you looking for an easy-to-use, free app to modify your image saturation levels and make your pictures truly pop? Look no further! Our...

Pixelate Image Tool

Introducing the ultimate free online image pixelator tool that allows you to easily transform your images into stunning pixel art in just a few...

Image RGB Level Adjustment Tool

Introducing the ultimate image color adjustment tool for all your photo editing needs. Our free online tool lets you take full control of your...

Image Color Inverter

Looking for a quick and efficient way to convert your images into negatives? Our Free Image to Negative Converter is the answer! Our online...

Latest Posts

Latest Questions