最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to redirect automatically after submit Google Recaptcha? - Stack Overflow

programmeradmin0浏览0评论

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" >
</form>

How to redirect to a page after submitting 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" >
</form>

How to redirect to a page after submitting Google Recaptcha?

Share Improve this question edited Oct 29, 2016 at 20:56 Stacked 7,3367 gold badges62 silver badges76 bronze badges asked Oct 29, 2016 at 17:53 ZaynZayn 713 silver badges12 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Read the docs:

https://developers.google./recaptcha/docs/verify

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 pletes 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

URL: https://www.google./recaptcha/api/siteverify

METHOD: POST

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('https://www.google./recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

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

// close the connection, release resources used
curl_close($ch);

// do anything you want with your response
var_dump(json_decode($response));

In javascript, google recaptcha has a callback response method grecaptcha.getResponse(). you can check its length which is > 0 if captcha was successful.

if(grecaptcha.getResponse().length === 0) { //redirect code here }

here is a full working example for php v7.x using reCaptchaV2; wrote based under the reply from this own page from "meda", "Alive to Die" under the page How to json decode in php and the Google reCaptcha v2 own example from https://developers.google./recaptcha/docs/display. i just have put the pieces together and then i got that. Tks to the contributors!

    <html>
      <head>
        <title>reCAPTCHA V2 demo by softlivre..br</title>
         <script src="https://www.google./recaptcha/api.js" async defer></script>
      </head>
      <body>
        <form action="./index.php" method="POST">
          <!-- here you must input the site key, not the secret key -->
          <div class="g-recaptcha" data-sitekey="xxxxxxxxxxxx"></div>
          <br/>
          <input type="submit" value="Submit">
        </form>
      </body>

    <?php
    // here you must input the secret key, not the site key
    // don´t worry, it is server side protected and won´t be
    // visible under the page source, it´s php code from now on...
    $secret = "yyyyyyyyyyyyy";

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

    $ch = curl_init('https://www.google./recaptcha/api/siteverify');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

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

    // close the connection, release resources used
    curl_close($ch);

    // do anything you want with your response
    // var_dump(json_decode($response)); // unment this to get the json full response
    $array = json_decode($response,true);
    //echo "<pre/>";print_r($array); // unment this to get the json to array full response/results

    if($array['success'] == 1){
    // here we have confirmed the chalenge, do whatever you want here, as redirecting to another
    // page. i suggest using $_SESSION in order for really protecting the other page to be
    // redirected from here to be safe, else anyone may access the other page directly 
    // without passing by the recapctha challenge, so there won´t be any point in this effort!
        echo "success!" ;
    }
    else{
        echo "Challenge not accepted so far....";
    }
    ?>

    </html>
<html>
  <head>
    <title>reCAPTCHA V2 demo by softlivre..br</title>
     <script src="https://www.google./recaptcha/api.js" async defer></script>
  </head>
  <body>
    <form action="./index.php" method="POST">
      <!-- here you must input the site key, not the secret key -->
      <div class="g-recaptcha" data-sitekey="xxxxxxxxxxxx"></div>
      <br/>
      <input type="submit" value="Submit">
    </form>
  </body>

<?php
// here you must input the secret key, not the site key
// don´t worry, it is server side protected and won´t be
// visible under the page source, it´s php code from now on...
$secret = "yyyyyyyyyyyyy";

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

$ch = curl_init('https://www.google./recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

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

// close the connection, release resources used
curl_close($ch);

// do anything you want with your response
// var_dump(json_decode($response)); // unment this to get the json full response
$array = json_decode($response,true);
//echo "<pre/>";print_r($array); // unment this to get the json to array full response/results

if($array['success'] == 1){
// here we have confirmed the chalenge, do whatever you want here, as redirecting to another
// page. i suggest using $_SESSION in order for really protecting the other page to be
// redirected from here to be safe, else anyone may access the other page directly 
// without passing by the recapctha challenge, so there won´t be any point in this effort!
    echo "success!" ;
}
else{
    echo "Challenge not accepted so far....";
}
?>

</html>
发布评论

评论列表(0)

  1. 暂无评论