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

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

Whats the difference between the Tapo P100 and the P105?

There are a few different Tapo smart plugs. The P100 and P110 differ based on the smart power monitoring feature but where does the...

Time remaining keeps going up on Whirlpool dryer??

I have clothes in the dryer regularly and the time remaining shows on the display. There could be 10 mins remaining and if I...

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

Memory Converter

Converting values between various metric measurements is usually quite simple as there will be 1000 of the smaller unit in the next larger unit....

Bitrate Converter

Below you will find a bitrate converter. This tool will allow you to enter a bitrate value, in one of many different formats and...

Aesthetic Text Generator

There are various ways to make your social media profile seem more unique, some of which are not as easy to implement as others....

Aspect Ratio Calculator For Images

Aspect ratio is the ratio between the height and width of an image. If you want to resize an image by 100 pixels, you...

Add Text To Image

Use this free tool to add text to an image. Simply select the image file that you want to overlay text onto and you...

JavaScript Multi-line String Builder

Javascript did not always support multi-line strings. If you attempted to create a string variable using quotes, putting a line break into the source...

Latest Posts

Latest Questions