How to setup Google reCAPTCHA

Oct 13, 2023
Tech
How to setup Google reCAPTCHA

Step 1: Create a Google reCAPTCHA API Key

  1. Go to the reCAPTCHA website:

  2. Sign in with your Google Account:

    • Sign in with your Google Account or create one if you don't have it.
  3. Register a New Site:

    • Click on the "+" (plus) icon to create a new site.
  4. Choose reCAPTCHA Type:

    • Select the type of reCAPTCHA you want to use. "reCAPTCHA v2" is commonly used for websites. You may choose between the "I'm not a robot Checkbox" or "Invisible reCAPTCHA Badge" options.
  5. Add Domains:

    • Add the domain(s) where you'll be using reCAPTCHA. If applicable, you may also need to specify subdomains.
  6. Accept reCAPTCHA Terms of Service:

    • Accept the reCAPTCHA Terms of Service and click on the "Submit" button.
  7. Get API Keys:

    • Once registered, you'll be provided with a set of keys - a "Site Key" and a "Secret Key." You'll use these keys in your website's code.

Step 2: Integrate reCAPTCHA into Your Website

For reCAPTCHA v2 Checkbox:

Add the following script to your HTML just before the closing </head> tag:

html
<head> <!-- Other head elements --> <script src="https://www.google.com/recaptcha/api.js" async defer></script> </head>

Place the reCAPTCHA widget where you want the checkbox to appear in your HTML form:

html
<form action="your_form_processing_script.php" method="post"> <!-- Your form fields go here --> <div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div> <input type="submit" value="Submit"> </form>

Replace "YOUR_SITE_KEY" with the actual Site Key you obtained.

For Invisible reCAPTCHA:

Add the reCAPTCHA script to your HTML:

html
<head> <!-- Other head elements --> <script src="https://www.google.com/recaptcha/api.js" async defer></script> </head>

Add the reCAPTCHA widget to your form:

html
<form action="your_form_processing_script.php" method="post"> <!-- Your form fields go here --> <button class="g-recaptcha" data-sitekey="YOUR_SITE_KEY" data-callback="onSubmit" data-badge="inline">Submit</button> </form>

Replace "YOUR_SITE_KEY" with the actual Site Key you obtained.

Step 3: Verify reCAPTCHA on the Server

Verify reCAPTCHA on the server-side when processing the form submission. Send a request to Google's reCAPTCHA API to validate the user's response.

Here is a simplified example in PHP:

php
<?php // your_form_processing_script.php $recaptcha_secret = "YOUR_SECRET_KEY"; $response = $_POST['g-recaptcha-response']; $verify_url = "https://www.google.com/recaptcha/api/siteverify?secret={$recaptcha_secret}&response={$response}"; $verify = file_get_contents($verify_url); $result = json_decode($verify); if ($result->success) { // Process the form submission // ... } else { // Handle failed reCAPTCHA verification // ... } ?>

Replace "YOUR_SECRET_KEY" with your actual Secret Key.

Buysalesbd online shopping Mall | AN ONLINE SHOPPING PLATFORM WITH GREAT DEALS

All categories
Flash Sale
Todays Deal
Auction