Mr Zy Mr Zy - 1 year ago 172
PHP Question

How to redirect automatically after submit Google Recaptcha?

I want to create a form with Google Recaptcha, but I don't know how to implement this case.

This is my HTML code

<form action="redirect.php" method="post">
<div class="g-recaptcha" data-sitekey="xxxxxxxxxx"></div>
<input type="submit" value="Submit" >

How to redirect to a page after submitting Google Recaptcha?

Answer Source

Read the docs:

Verifying the user's response

This page explains how to verify a user's response to a reCAPTCHA challenge from your application's backend. When a reCAPTCHA is solved by end user, a new field (g-recaptcha-response) will be populated in HTML. You can get the user’s response in one of three ways:

g-recaptcha-response POST parameter when the user submits the form on your site grecaptcha.getResponse(opt_widget_id) after the user completes the CAPTCHA challenge As a string argument to your callback function if data-callback is specified in either the g-recaptcha tag attribute or the callback parameter in the grecaptcha.render method Each reCAPTCHA response is a token that should be used only once. If a verification attempt has been made with a particular token, it cannot be used again. You will need to call grecaptcha.reset() to ask the end user to verify with reCAPTCHA again.

After you get the response token, you need to verify it with reCAPTCHA using the following API to ensure the token is valid.

API Request



POST Parameter Description secret Required. The shared key between your site and reCAPTCHA. response Required. The user response token provided by reCAPTCHA, verifying the user on your site. remoteip Optional. The user's IP address. API Response

The response is a JSON object:

  "success": true|false,
  "challenge_ts": timestamp,  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
  "hostname": string,         // the hostname of the site where the reCAPTCHA was solved
  "error-codes": [...]        // optional

PHP example:

// set post fields
$post = [
    'secret' => $secret,
    'response' => $_POST['g-recaptcha-response'],
    'remoteip'   => $_SERVER['REMOTE_ADDR']

$ch = curl_init('');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

// execute!
$response = curl_exec($ch);

// close the connection, release resources used

// do anything you want with your response
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download