reCAPTCHA is a service offered by Google that helps protect websites from abuse and spam by verifying whether a user is a human or a bot. This system secures online forms and other sensitive features.
- Website Form:
To enable the "reCAPTCHA" option, please follow these steps:
- Go to the Google reCAPTCHA website:
- You will need to register the following information as shown in the image below, then click "SEND":
- Label: Give a name to your reCAPTCHA (e.g., "My Site").
- Type of reCAPTCHA: Choose between reCAPTCHA v2 (checkbox) or reCAPTCHA v3.
- Domains: Add the domains where you will use reCAPTCHA.
- After that, copy the keys as mentioned in the next image :
Site Key: This key is used in your website's HTML code.
Secret Key: This key is used on your server to validate the responses.
- You need to add the following code to your HTML file where you want the reCAPTCHA to appear (for reCAPTCHA v2):
<form action="your_processing_script.php" method="POST">
<!-- Your form fields here -->
<div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>
<input type="submit" value="Submit">
</form>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
- Then, in your processing script (e.g.,
your_processing_script.php
), add the following code to validate the response:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$recaptchaSecret = 'YOUR_SECRET_KEY';
$recaptchaResponse = $_POST['g-recaptcha-response'];
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$recaptchaSecret&response=$recaptchaResponse");
$responseKeys = json_decode($response, true);
if (intval($responseKeys["success"]) !== 1) {
echo 'reCAPTCHA verification failed. Please try again.';
} else {
// Process the form
echo 'Form submitted successfully.';
}
}?>
Make sure to test the reCAPTCHA by submitting the form to verify that it works correctly.
- WordPress Form:
Access Google reCAPTCHA: Visit the Google reCAPTCHA website.
-
Create a Project:
- Label: Give a name to your project (e.g., "My WordPress Site").
- Type of reCAPTCHA: Choose between reCAPTCHA v2 (with checkbox) or v3 (without interaction).
- Domains: Add your WordPress site domain.
- Agree to the terms and click "SEND."
- Retrieve Keys: Note down your Site Key and Secret Key.
- Install the Plugin: Install a reCAPTCHA plugin of your choice.
- Configure the Plugin: Customize the appearance and behavior of the reCAPTCHA according to the plugin selected. Refer to the plugin documentation for further details.
- Test Your reCAPTCHA: Go to the form on your site. Ensure the reCAPTCHA appears correctly and test its functionality by submitting the form.