March 10, 2025

How to Create a reCAPTCHA Key and Secret Port

reCAPTCHA is an essential tool for protecting websites from spam and abuse by distinguishing between humans and bots. Google reCAPTCHA offers different versions, including v2 and v3, each suited for different security needs. This guide will walk you through the process of creating a reCAPTCHA key and secret key to integrate into your website.

Step-by-Step Guide to Generating a reCAPTCHA Key and Secret Key

Step 1: Visit the Google reCAPTCHA Website

  1. Open your browser and go to Google reCAPTCHA.
  2. Click on the Admin Console button at the top right.

Step 2: Register a New Site

  1. Sign in with your Google account.
  2. Enter a label to identify the site (e.g., “My Website reCAPTCHA”).
  3. Choose the type of reCAPTCHA:
    • reCAPTCHA v2: Offers a checkbox challenge, an invisible verification, or an Android-specific solution.
    • reCAPTCHA v3: Works in the background and assigns a score based on the user’s behavior.
  4. Enter your website domain (e.g., example.com).
  5. Accept the reCAPTCHA Terms of Service.
  6. Click Submit to generate the keys.

Step 3: Copy the reCAPTCHA Keys

  1. After submitting, Google will provide you with two keys:
    • Site Key: Used in the front-end HTML code.
    • Secret Key: Used in the back-end to verify user responses.
  2. Keep the Secret Key confidential to prevent unauthorized access.

Step 4: Implement reCAPTCHA on Your Website

  1. Add the following script in the <head> section of your HTML:
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  2. For reCAPTCHA v2 (Checkbox), use the following code where you want the reCAPTCHA to appear:
    <form action="verify.php" method="POST">
        <div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>
        <br>
        <input type="submit" value="Submit">
    </form>
  3. For reCAPTCHA v3, use the following:
    <script>
        grecaptcha.ready(function() {
            grecaptcha.execute('YOUR_SITE_KEY', {action: 'homepage'}).then(function(token) {
                document.getElementById("recaptchaResponse").value = token;
            });
        });
    </script>
    <input type="hidden" id="recaptchaResponse" name="recaptchaResponse">

Step 5: Verify reCAPTCHA Response in Backend

  1. In PHP, you can verify the response using the Secret Key:
    <?php
    $secretKey = "YOUR_SECRET_KEY";
    $response = $_POST['g-recaptcha-response'];
    $remoteIP = $_SERVER['REMOTE_ADDR'];
    
    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $data = ['secret' => $secretKey, 'response' => $response, 'remoteip' => $remoteIP];
    
    $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);
    $jsonResult = json_decode($result);
    
    if ($jsonResult->success) {
        echo "Verification successful!";
    } else {
        echo "Verification failed!";
    }
    ?>

Conclusion

Integrating Google reCAPTCHA into your website enhances security and reduces spam attacks. By following these steps, you can successfully generate and implement reCAPTCHA keys for your website.

Sumit Kumar Jha Web Developer

With over 4 years of industry expertise, SD Cares Enterprises is dedicated to delivering top-notch services. We pride ourselves on our commitment to excellence, ensuring that every project is handled with the utmost care and professionalism.

Website: https://sdcaresenterprises.com

Leave a Comment

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


Latest Updates Find something that might like you

Web Security Focus: OAuth 2.1 Adoption Grows

Web Security Focus: OAuth 2.1 Adoption Grows As web applications become increasingly sophisticated and integrated into our daily lives, the...

August 30, 2025

Next.js 15 Optimizes Server-Side Rendering with Partial Pre-Rendering and Improved Caching

Next.js 15 Optimizes Server-Side Rendering with Partial Pre-Rendering and Improved Caching The release of Next.js 15 (currently in beta) is...

August 30, 2025

CSS Container Queries Gain Browser Support: A New Era for Responsive Design

CSS Container Queries Gain Browser Support: A New Era for Responsive Design Responsive web design is undergoing a major transformation...

August 30, 2025

Node.js 22 Introduces Experimental Features for the Future of Backend Development

Node.js 22 Introduces Experimental Features for the Future of Backend Development The release of Node.js 22 marks a significant step...

August 30, 2025

Progressive Web Apps See a Resurgence in 2025 with Improved Browser Support

Progressive Web Apps See a Resurgence in 2025 with Improved Browser Support Progressive Web Apps (PWAs) are staging a powerful...

August 30, 2025