Using new Google reCAPTCHA using PHP

In the tutorial, I will be using a working HTML contact form. We will submit it post method process the form values with PHP.

Google has released the new reCAPTCHA. They need just a single click to confirm they are not a robot. So, reCAPTCHA will protect your website from spam with better user experience. You can easily integrate Google reCAPTCHA in PHP script.

Header file Code

<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

HTML Code

<form action="submit.php" method="POST">
	<input type="text" required="" class="text" value="" placeholder="Your name" name="name" >
	<input type="email" required="" class="text" value="" placeholder="Email adress" name="email" >
	<textarea type="text" placeholder="Message..." required="" name="message"></textarea>
	<div class="g-recaptcha" data-sitekey=""></div>
	<input type="submit" name="submit" value="SUBMIT">
</form>

PHP Code

<?php
	$secret = '';
	$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
	$responseData = json_decode($verifyResponse);
	if($responseData->success)
	{
		echo "Success";
	}
	else
	{
		echo "Google reCAPTCHA Expired!!";
	}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *