READ MORE & ENJOY 🤗

Blog Details

Blog Image

Google reCAPTCHA with PHP

In today's digital landscape, website security is a top priority. One effective way to protect your website from spam, bots, and malicious activity is by implementing Google reCAPTCHA. reCAPTCHA is a free service provided by Google that adds an extra layer of security to your web forms. In this blog post, I'll guide you through the process of integrating Google reCAPTCHA with PHP to enhance the security of your website's contact form.

What is Google reCAPTCHA?

Google reCAPTCHA is a widely used tool designed to distinguish between humans and bots. It presents users with a challenge, such as selecting images or solving puzzles, to prove that they are not automated scripts. By adding reCAPTCHA to your website, you can prevent automated form submissions, comment spam, and other unwanted activities.


Prerequisites

Before we start integrating reCAPTCHA, you should have the following:

  • - A PHP-enabled web server.
  • - A Google account to register your website and obtain reCAPTCHA keys.
  • - Basic knowledge of HTML, PHP, and JavaScript.


Step 1: Register Your Website with Google reCAPTCHA

  1. 1- Visit the reCAPTCHA website.
  2. 2- Click on the "Admin Console" button and sign in with your Google account.
  3. 3- Register a new site by filling in your website's details. You'll receive two keys: a site key and a secret key.


Step 2: Adding reCAPTCHA to Your Contact Form

In your HTML form, add the reCAPTCHA widget just before the form's submit button. Replace "YOUR_SITE_KEY_HERE" with your actual site key.

HTML Code:

<div class="form-group mb-4"> <!-- Google reCAPTCHA block --> <div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY_HERE"></div> </div>

Then load the reCAPTCHA API JavaScript library, you can add it in the footer with all your scripts.

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


Step 3: Verify reCAPTCHA in PHP

In your PHP processing script (e.g., process.php), you need to verify the reCAPTCHA response before processing the form data. Use the secret key you obtained earlier.

PHP Code:
$secretKey = "YOUR_SECRET_KEY_HERE";
$responseKey = $_POST['g-recaptcha-response'];

$url = "<https://www.google.com/recaptcha/api/siteverify>";
$data = [
    'secret' => $secretKey,
    'response' => $responseKey,
    'remoteip' => $_SERVER['REMOTE_ADDR']
];

$options = [
    'http' => [
        '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, true);

if ($response["success"] === true) {
    // reCAPTCHA verification passed; process your form data
    // Insert your form processing logic here
    echo "Form submitted successfully!";
} else {
    // reCAPTCHA verification failed
    echo "reCAPTCHA verification failed. Please try again.";
}


Step 4: Test Your Contact Form

Test your contact form with the reCAPTCHA integration to ensure that it's working as expected. You should now have an enhanced level of security on your website's contact form, protecting it from spam and bots.


Conclusion

Integrating Google reCAPTCHA with PHP is a simple yet effective way to secure your website's forms. By following the steps outlined in this blog post, you can significantly reduce unwanted spam submissions and ensure that your website remains safe and user-friendly.

Enhance your website's security today with Google reCAPTCHA, and provide your users with a seamless and secure online experience.

Connect with Us

Let’s Talk About how I can help!🚀

If you like my work and want to avail my services then drop me a message using the contact form. Feel free to reach out to us.