Add Google reCAPTCHA V3 to a Website (Protect Froms from Spam)
A reCAPTCHA acts as an extra layer for HTML forms and protects websites against spam form submissions made by spam bots. Learn how to protect forms by integrating Google reCAPTCHA v3 into a website.

Spam bots are automated programs that are designed to create deceptive content or perform automated, disruptive, fraudulent, and abusive activities across online platforms. For example, creating fake accounts, sending spam emails, tricking users to share their personal information, and sending fake offers, etc. Different technology providers offer CAPTCHA services such as Cloudflare's Turnstile Checkbox, Google's reCAPTCHA V2, and hCAPTCHA to help websites protect forms against spam bots. This post walks through the steps required to integrate Google invisible reCAPTCHA into a website.
How to Integrate Google Invisible reCAPTCHA V3 into a Website?
Google reCAPTCHA v3 is an invisible CAPTCHA offered by Google that does not require users to complete a challenge for human verification. It rather runs quietly in the background and evaluates a risk score (from 0.0 to 1.0) based on user interaction, and the website decides on the backend what action to take based on this score. The 0.0 indicates very likely a bot and 1.0 indicates very likely to be human user. It can be added to a website in the following steps:
Step 1: Create a Google reCAPTCHA Account
The first step is to create an account on the Google reCAPTCHA console and retrieve the site key and site secret that are required for server-side verification.
- Go to Google reCAPTCHA and register a new site page.
- Enter the label for the website and fill out the other fields.
- Choose the reCAPTCHA v3 score based checkbox.
- Enter the domain names that are allowed to use this service.
- Select the project and click the "Submit" button.
- Google will display the site key and secret key on the next page. Save these keys for use on the website.
Step 2: Add Invisible reCAPTCHA to HTML Page
The next step is to add the reCAPTCHA API JS to the HTML that contains a form. We will still need to initialize the service in the next step:
- Create an HTML page that contains a form.
- Add all the required form fields. For demo purposes, we are adding only one field to the form.
- Add the reCAPTCHA API script tag along with the
render=YOUR_SITE_KEYquery parameter to the head element of the page.
index.html
<!DOCTYPE html>
<html>
<head>
<title>Add Google reCAPTCHA V3 to a Website - Demo</title>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport" />
<link rel="stylesheet" href="css/style.css" />
<script src="https://www.google.com/recaptcha/api.js?render=GOOGLE_RECAPTCHA_V3_SITE_KEY" async defer></script>
<script src="js/javascript.js" type="text/javascript"></script>
</head>
<body>
<section class="section py-4">
<div class="container">
<form class="captcha-form" method="POST" action="process-form.php">
<div class="mb-4">
<label class="inline-block mb-1">Name: <span class="text-red">*</span></label>
<input type="text" name="name" class="form-control" required="required" placeholder="Enter name..." />
</div>
<button type="submit" class="btn btn-blue btn-recaptcha-v2">
<i class="fa fa-save"></i>
Submit
</button>
</form>
</div>
</section>
</body>
</html>
Step 3: Render reCAPTCHA V3 on Form Submit
We initialize the reCAPTCHA service in this step, add an event listener to a form, and dynamically add a token input field with JavaScript that will be submitted with the form. For demo purposes, we are adding the event listener to all forms.
- Add a
DOMContentLoadedevent listener to the document to ensure the DOM tree is ready. - Select all form elements and attach the submit event listener to the form.
- Prevent the form submission using
e.preventDefault()to render the reCAPTCHA before form submission. - Render the hidden reCAPTCHA token field using
grecaptcha.execute()method. Use the site key from the reCAPTCHA console as a parameter. - Finally, submit the form using the
form.submit()function.
javascript.js
document.addEventListener("DOMContentLoaded", function() {
[].slice.call(document.querySelectorAll("form")).forEach(function (form) {
form.addEventListener("submit", function (e) {
e.preventDefault();
grecaptcha.execute("GOOGLE_RECAPTCHA_V3_SITE_KEY", {
action: "submit"
}).then(function (token) {
// Add the token to the form
let input = document.createElement("input");
input.type = "hidden";
input.name = "recaptcha_token";
input.value = token;
form.appendChild(input);
form.submit();
});
});
});
});Step 4: Verify reCAPTCHA Token on Server Side (PHP)
This is our server-side script that is responsible for verifying the token with the Google reCAPTCHA verification API endpoint before processing the form. The script uses the cURL extension to make the API request along with the required data.
- Sanitize the
$_POSTarray using thefilter_input_array()function. - Check if the form data is not empty in the
$_POSTrequest. - The rest of the process is the same as Google reCAPTCHA V2 verification.
- Prepare an array of
$datato send to the verification API. - Initialize a cURL request and set required options for the API request.
- Execute the cURL request and decode the JSON response using the
json_decode()method. - Check if the response has a parameter
successand has the valuetrue/1indicating a successful verification. - After verifying the reCAPTCHA token, form data can be processed.
process-form.php
<?php
// Sanitize input array
$post = filter_input_array(INPUT_POST);
if(!empty($post)){
// The website secret key
$secret_key = 'GOOGLE_RECAPTCHA_V3_SECRET_KEY';
$recaptcha_token = $post['recaptcha_token'];
// Verify the reCAPTCHA response with Google
$verify_url = 'https://www.google.com/recaptcha/api/siteverify';
$data = [
'secret' => $secret_key,
'response' => $recaptcha_token,
'remoteip' => $_SERVER['REMOTE_ADDR']
];
// Send a POST request using cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $verify_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
if ($result['success']) {
// reCAPTCHA's verification succeeded, and we can process the form
echo 'reCAPTCHA verification successful!';
} else {
// reCAPTCHA's verification failed, redirect back or handle error as needed
echo 'reCAPTCHA verification failed!';
}
}
A human form submission can also be determined using the score parameter in the response. We can consider it to be a form submission made by a human if the score is equal to or above 0.5, like in the code snippet below:
$score = $result['score'];
if ($score >= 0.5) {
// User is likely human, and we can proceed with the form processing
} else {
// User might be a bot, and we need to take a different action (e.g., challenge, flag for review)
}
Using reCAPTCHA services is a simple and convenient way to detect bot form submissions. We demonstrated how to add Google's reCAPTCHA v3 to a website to distinguish between human and bot form submissions. It not only improves the security of a website but also provides a better user experience, as it runs in the background and the user does not have to complete any challenge.