|

|  How to Implement CAPTCHA Using Google reCAPTCHA API in PHP

How to Implement CAPTCHA Using Google reCAPTCHA API in PHP

October 31, 2024

Learn how to integrate Google's reCAPTCHA API in PHP to secure your site, prevent bots, and enhance user experience with our step-by-step guide.

How to Implement CAPTCHA Using Google reCAPTCHA API in PHP

 

Integrate Google reCAPTCHA in PHP

 

To successfully integrate Google reCAPTCHA into your PHP application, it's important to execute the necessary backend validation to ensure security and proper functionality. Below are steps to guide you through the process.

 

Include reCAPTCHA JavaScript

 

Place the reCAPTCHA script inside the <head> section of your HTML file. The reCAPTCHA widget should be added to your form.

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>reCAPTCHA Demo</title>
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
    <form action="submit.php" method="POST">
        <!-- Your form fields here -->
        <div class="g-recaptcha" data-sitekey="your-site-key"></div>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

 

Handle Form Submission in PHP

 

Upon form submission, the reCAPTCHA widget returns a token. This token needs to be verified on the server-side to ensure it’s valid. Gather the token from POST data for validation.

 

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $recaptcha_secret = 'your-secret-key';
    $recaptcha_response = $_POST['g-recaptcha-response'];
    
    // Verify the reCAPTCHA response
    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $data = [
        'secret' => $recaptcha_secret,
        'response' => $recaptcha_response
    ];
    
    $options = [
        'http' => [
            'method'  => 'POST',
            'content' => http_build_query($data)
        ]
    ];
    $context  = stream_context_create($options);
    $verify = file_get_contents($url, false, $context);
    $captcha_success = json_decode($verify);
    
    if ($captcha_success->success) {
        // Proceed with form handling
        echo "CAPTCHA verified successfully!";
    } else {
        // Handle invalid CAPTCHA
        echo "Error verifying CAPTCHA. Please try again.";
    }
}

 

Explanation of the PHP Code

 

  • The first check ensures that the form submission is done via the POST method to enhance security.
  • Use the `file_get_contents` function with a configured `stream_context` to make a POST request to Google’s reCAPTCHA API.
  • The verification response is a JSON object, which should be decoded to check the `success` property.
  • Proceed only if `success` is `true`. Otherwise, resend the form or display an error message to users.

 

Testing and Debugging

 

  • It's crucial to test the reCAPTCHA on both desktop and mobile devices since the interaction might differ slightly.
  • Verify network requests using browser developer tools to ensure that the token is sent and received correctly.

 

Understanding and properly implementing Google reCAPTCHA on the server-side helps in significantly reducing spam and automated submissions in web applications, thereby securing user data and resources.

Pre-order Friend AI Necklace

Pre-Order Friend Dev Kit

Open-source AI wearable
Build using the power of recall

Order Now

OMI AI PLATFORM
Remember Every Moment,
Talk to AI and Get Feedback

Omi Necklace

The #1 Open Source AI necklace: Experiment with how you capture and manage conversations.

Build and test with your own Omi Dev Kit 2.

Omi App

Fully Open-Source AI wearable app: build and use reminders, meeting summaries, task suggestions and more. All in one simple app.

Github →

Join the #1 open-source AI wearable community

Build faster and better with 3900+ community members on Omi Discord

Participate in hackathons to expand the Omi platform and win prizes

Participate in hackathons to expand the Omi platform and win prizes

Get cash bounties, free Omi devices and priority access by taking part in community activities

Join our Discord → 

OMI NECKLACE + OMI APP
First & only open-source AI wearable platform

a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded

OMI NECKLACE: DEV KIT
Order your Omi Dev Kit 2 now and create your use cases

Omi Dev Kit 2

Endless customization

OMI DEV KIT 2

$69.99

Perfect for developers and tech enthusiasts who want to start creating immediately.

Has 64gb of on-board memory, a speaker, vibration and a programmable button.

Comes fully assembled, doesn't require technical skills to set up.

 

IMPORTANT: On backorder. Shipping end of November 2024.

Learn more

Omi Dev Kit 2: build at a new level

Key Specs

OMI DEV KIT

OMI DEV KIT 2

Microphone

Yes

Yes

Battery

4 days (250mAH)

2 days (250mAH)

On-board memory (works without phone)

No

Yes

Speaker

No

Yes

Programmable button

No

Yes

Estimated Delivery 

-

1 week

What people say

“Helping with MEMORY,

COMMUNICATION

with business/life partner,

capturing IDEAS, and solving for

a hearing CHALLENGE."

Nathan Sudds

“I wish I had this device

last summer

to RECORD

A CONVERSATION."

Chris Y.

“Fixed my ADHD and

helped me stay

organized."

David Nigh

OMI NECKLACE: DEV KIT
Take your brain to the next level

LATEST NEWS
Follow and be first in the know

Latest news
FOLLOW AND BE FIRST IN THE KNOW

San Francisco

team@basedhardware.com
Title

Company

About

Careers

Invest
Title

Products

Omi Dev Kit 2

Openglass

Other

App marketplace

Affiliate

Privacy

Customizations

Discord

Docs

Help