Home > Blog > How to protect your website with reCAPTCHA

How to protect your website with reCAPTCHA

reCAPTCHA is like a game that helps the computer know if you are a real person or a robot. When you go to some websites, they ask you to solve a puzzle or pick some pictures to show that you are a smart human. By playing these little games, you help the computer be sure it’s talking to a real person and not a tricky robot. It’s like a special secret code that only humans can solve. So, when you see reCAPTCHA, just have fun with the games and show the computer you’re a smart little human!

reCAPTCHA is a system developed by Google to distinguish between humans and automated bots on the internet. It is a security measure designed to prevent malicious activities such as spamming, data scraping, and unauthorized access to websites.

The name “reCAPTCHA” is a combination of the words “captcha” (which stands for Completely Automated Public Turing test to tell Computers and Humans Apart) and “re-” (meaning “again” or “once more”). The system was initially created by researchers at Carnegie Mellon University, and it was acquired by Google in 2009.

Traditional CAPTCHAs often involve displaying distorted or obscured text that users need to decipher and enter correctly to prove their human identity. However, reCAPTCHA introduced a more user-friendly approach by using a range of challenges that are easier for humans to solve but difficult for bots. These challenges include identifying objects in images, selecting specific images from a grid, or solving puzzles.

By solving reCAPTCHA challenges, users not only prove that they are human but also contribute to the digitization and improvement of various datasets. For example, when identifying objects in images, users help train machine learning models for computer vision tasks.

reCAPTCHA has become widely used across the internet and can be found on numerous websites as an added layer of security. It has evolved over time to adapt to new challenges and improve its effectiveness in differentiating between humans and bots, while also providing a better user experience.

To set up reCAPTCHA in PHP, you need to follow these steps:

  1. Sign up for reCAPTCHA:
    • Go to the reCAPTCHA website: https://www.google.com/recaptcha
    • Click on “Admin Console” in the top-right corner.
    • Sign in with your Google account or create a new account if you don’t have one.
    • Register a new site by providing a label and the domain name where you want to use reCAPTCHA.
  2. Obtain API keys:
    • After registering the site, you’ll receive two keys: Site Key and Secret Key.
    • The Site Key will be used in the HTML code, and the Secret Key will be used in the server-side validation.
  3. Add reCAPTCHA to your HTML form:
    • In the HTML form where you want to use reCAPTCHA, add the following code inside the <form> tag:
<div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>

4. Replace YOUR_SITE_KEY with the Site Key you obtained in the previous step. Include the reCAPTCHA library. In the <head> section of your HTML file, add the following code to include the reCAPTCHA JavaScript library:

<script src="https://www.google.com/recaptcha/api.js" async defer></script>

5. Server-side validation with PHP:

  • In your PHP script that handles the form submission, you need to validate the reCAPTCHA response.
  • Retrieve the user’s response from the form submission using the $_POST superglobal:
$recaptchaResponse = $_POST['g-recaptcha-response'];

Verify the response with Google’s reCAPTCHA API by sending a POST request to the API endpoint using cURL or a similar method:

$url = 'https://www.google.com/recaptcha/api/siteverify';
$secretKey = 'YOUR_SECRET_KEY';
$data = array(
    'secret' => $secretKey,
    'response' => $recaptchaResponse
);

$options = array(
    'http' => array(
        'header' => "Content-type: application/x-www-form-urlencoded\r\n",
        'method' => 'POST',
        'content' => http_build_query($data)
    )
);

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result);

if ($response->success) {
    // reCAPTCHA verification succeeded, continue processing the form data
} else {
    // reCAPTCHA verification failed, handle the error
}

6. Replace YOUR_SECRET_KEY with the Secret Key you obtained in step 2.

Process the form submission:

  1. If the reCAPTCHA verification succeeds, you can proceed with processing the rest of the form data as per your requirements.
  2. If the reCAPTCHA verification fails, you can display an error message to the user or take appropriate action.

That’s it! You have now set up reCAPTCHA in your PHP form. The reCAPTCHA verification helps protect your form from automated spam and abuse.

Leave a Reply

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