How to Validate Google reCaptcha on Form Submit

How to Validate Google reCaptcha on Form Submit

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Validate Google reCaptcha on Form Submit

Google recently overhauled its reCAPTCHA API, consolidating the complex widget options into a single checkbox experience. While this simplifies the frontend implementation—often as simple as placing a <div> with data-sitekey in your HTML—it introduces a specific challenge for backend developers: how to actually validate that the user completed the challenge before processing their submission.

If you are following the standard migration path, the answer is yes, you still need a backend verification step. The frontend widget cannot guarantee security on its own; it relies entirely on the server to confirm the response token with Google's servers. If your form currently submits data without checking this token, you are effectively disabling the reCAPTCHA protection.

The Backend Verification Endpoint

The core mechanism remains unchanged from previous versions: you must send the response token generated by the widget to a verification endpoint. This is where the private key comes into play. Google provides two primary methods for fetching this verification result, but the HTTP POST method is generally preferred for server-side validation.

You need to create an endpoint in your application that accepts the form data and the reCAPTCHA response token. The request must include three specific parameters:

  • secret: Your private key (kept confidential on your server).
  • response: The token generated by the widget.
  • remoteip (optional): The user's IP address, which can improve accuracy if provided.

The response from Google will be a JSON object containing a boolean success field and an optional action_reason if the verification fails. If success is true, you may then proceed to process the form data.

Implementing Verification in Laravel

Since this guide focuses on practical implementation, consider how this logic fits into a modern framework like Laravel. You don't need to reinvent the wheel; there are robust packages that handle the HTTP request and JSON parsing for you, reducing boilerplate code and potential errors.

A popular choice is barryvdh/laravel-recaptcha. Installing this via Composer allows you to access the verification logic easily within your controllers or services. Here is a conceptual example of how you might structure a controller method using such a package:

use Barryvdh\ReCaptcha\Facades\ReCaptcha;

public function store(Request $request)
{
    // Validate standard form fields...

    // Verify the reCAPTCHA token
    if (! ReCaptcha::verify($request->get('g-recaptcha-response'))) {
        return back()->withErrors(['recaptcha' => 'reCAPTCHA verification failed.']);
    }

    // Proceed with saving the form data
    $this->form->create([...]);

    return redirect('/success');
}

If you prefer not to use a package, you can perform the HTTP request manually using Guzzle or PHP's built-in cURL. The key is ensuring that your route handling the submission does not bypass this check. In many cases, developers mistakenly treat the frontend widget state as sufficient proof of human interaction, leading to automated bot submissions slipping through.

Configuration and Environment Variables

Security best practices dictate that you never hardcode your private key directly into your application files or .env file if it can be avoided, though for small applications, an environment variable is acceptable. Ensure your server environment exposes the RECAPTCHA_SECRET_KEY variable to whichever service makes the HTTP request.

When integrating this into a larger system, remember that the verification endpoint must be reachable by your application code running on your server. It does not need to be a public-facing route; it is an internal utility function that accepts POST requests containing the token and secret.

By implementing this validation step, you ensure that the simplified checkbox interface provides the intended security layer, preventing automated scripts from abusing your form submission endpoints.

Our network