Google has released the new reCAPTCHA v2 checkbox widget. Using the reCAPTCHA widget users can prove that they are human without solving a CAPTCHA question. Only a single click is needed to confirm they are not a robot. The Google reCAPTCHA Checkbox protects your website from spam with a better user experience. Generally, Google reCAPTCHA functionality is used in the web form to protect from spam. In addition, the Google reCAPTCHA can also be used to protect websites from spam requests. The reCAPTCHA helps to validate any response coming from the website that is useful for bot protection.
Google reCAPTCHA API provides an easy and effective way to protect your web form or web page from getting spam. Google reCAPTCHA can be easily integrated into the website using PHP. In this tutorial, we will show you how to integrate Google reCAPTCHA checkbox with PHP in the web form.
In the example code, the following functionality will be implemented to demonstrate the Google reCAPTCHA integration with PHP.
The reCAPTCHA keys are required to call the Google reCAPTCHA API. Before adding the reCAPTCHA v2 checkbox to your site, you need to register your site and get reCAPTCHA API keys.
Register Website:
Register the domain of your website at Google reCAPTCHA Admin console.
Get Site Key and Secret Key:
After the submission, your site will be added and the reCAPTCHA keys will be generated. The Site Key and Secret Key need to be specified in the script at the time of calling Google reCAPTCHA API.
Copy the Site Key and Secret Key for later use in the Google reCAPTCHA integration code.
First, include the JavaScript library of reCAPTCHA API.
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
Add the g-recaptcha tag element in the HTML form where you want to display the reCAPTCHA checkbox widget.
g-recaptcha
DIV element has a class (named “g-recaptcha”) and data-sitekey
attributes.<form action="" method="post">
<div class="input-group">
<input type="text" name="name" value="" placeholder="Your name" required="" />
</div>
<div class="input-group">
<input type="email" name="email" value="" placeholder="Your email" required="" />
</div>
<div class="input-group">
<textarea name="message" placeholder="Type message..." required="" ></textarea>
</div>
<!-- Google reCAPTCHA box -->
<div class="g-recaptcha" data-sitekey="Your_reCAPTCHA_Site_Key"></div>
<!-- Submit button -->
<input type="submit" name="submit" value="SUBMIT">
</form>
After the form submission, the input data will be submitted to the server-side script to verify the user’s response and process the contact request.
g-recaptcha-response
POST parameter to check whether the user selects reCAPTCHA checkbox.secret
) & user’s response (response
). Check the reCAPTCHA API response.<?php
// Google reCAPTCHA API keys settings
$secretKey = 'Your_reCaptcha_Secret_Key';
// Email settings
$recipientEmail = 'admin@example.com';
// If the form is submitted
$postData = $statusMsg = '';
$status = 'error';
if(isset($_POST['submit'])){
$postData = $_POST;
// Validate form input fields
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message'])){
// Validate reCAPTCHA checkbox
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
// Verify the reCAPTCHA API response
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secretKey.'&response='.$_POST['g-recaptcha-response']);
// Decode JSON data of API response
$responseData = json_decode($verifyResponse);
// If the reCAPTCHA API response is valid
if($responseData->success){
// Retrieve value from the form input fields
$name = !empty($_POST['name'])?$_POST['name']:'';
$email = !empty($_POST['email'])?$_POST['email']:'';
$message = !empty($_POST['message'])?$_POST['message']:'';
// Send email notification to the site admin
$to = $recipientEmail;
$subject = 'New Contact Request Submitted';
$htmlContent = "
<h4>Contact request details</h4>
<p><b>Name: </b>".$name."</p>
<p><b>Email: </b>".$email."</p>
<p><b>Message: </b>".$message."</p>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From:'.$name.' <'.$email.'>' . "\r\n";
// Send email
mail($to, $subject, $htmlContent, $headers);
$status = 'success';
$statusMsg = 'Thank you! Your contact request has been submitted successfully.';
$postData = '';
}else{
$statusMsg = 'Robot verification failed, please try again.';
}
}else{
$statusMsg = 'Please check the reCAPTCHA checkbox.';
}
}else{
$statusMsg = 'Please fill all the mandatory fields.';
}
}
?>
Use the following code to display the form submission status message in the HTML form section.
<?php if(!empty($statusMsg)){ ?>
<p class="status-msg <?php echo $status; ?>"><?php echo $statusMsg; ?></p>
<?php } ?>
Google Invisible reCAPTCHA with PHP
There are multiple options are available to add CAPTCHA functionality to the website. The Google reCAPTCHA is the easiest option to add a CAPTCHA challenge to the form on the website. You can also, see the analytics reports of the CAPTCHA Requests in the reCAPTCHA admin panel. In the example code, we have shown how you can add Google reCAPTCHA checkbox in the contact form. Not only the contact form but also you can use our code to integrate Google reCAPTCHA into any type of form in the web application.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
Hi, I have a problem with the contact form.
I followed all the instructions:
1. Register your site in the Google reCAPTCHA admin console – https://www.google.com/recaptcha/admin
2. Collect the site key and secret key.
3. In the “submit.php” file:
===> Specify the site key ($siteKey) and secret key ($secretKey).
===> Specify the recipient’s email address in the $recipientEmail variable to receive email notification when the contact form is submitted.
But still it doesn’t work when I access the site in the form there is always written “ERROR for the site owner: invalid site key”.
As the error message says, you have not set the correct Site Key in the reCAPTCHA container element. You need to specify the Site Key in the
data-sitekey
attribute.Works great but on iOS16 doesn’t work!!! Any ideas?