【问题标题】:ReCAPTCHA doesn't work on localhostReCAPTCHA 在本地主机上不起作用
【发布时间】:2015-02-25 22:07:49
【问题描述】:

我正在使用带有 reCAPTCHA 检查的表单构建一个网站。根据Google documentation 的要求,我为目标域创建了一个密钥。然后,我创建了一个包含 reCAPTCHA 部分的表单

HTML 表单

<form method="post" action="index.php">
   <div class="g-recaptcha" data-sitekey="PUBLIC_KEY"></div>
   <input type="submit" name="submit" />
</form>


PHP 响应检查

提交表单时,验证 reCAPTCHA 响应(在此示例中,它只是打印)。

$recaptcha = filter_input(INPUT_POST, 'g-recaptcha-response', FILTER_SANITIZE_STRING);
$googleurl = "https://www.google.com/recaptcha/api/siteverify";
$privatekey = "PRIVATE_KEY";
$remoteip = $_SERVER['REMOTE_ADDR'];

$curl = new Curl($googleurl."?secret=".$privatekey."&response=".$recaptcha."&remoteip=".$remoteip);
$response = json_decode($curl->exec(), true);

print_r($response);
die();

Curl 是一个简单构建 curl 请求并返回结果的类。

问题

sn-p 在线工作正常,我检查了$response 值,包括成功和错误情况。但是在开发过程中,我也必须在 localhost 上使用它。如this post 中所述,所有密钥都应在本地工作。但是当我运行代码时,什么都没有显示。

【问题讨论】:

    标签: api google-api localhost recaptcha


    【解决方案1】:

    虽然这个问题比较老,但我发布了答案,因为很多人可能会遇到同样的问题。我认为这可能与在 localhost 上运行 reCaptcha 的一般身份验证问题有关,可以使用 secure token

    解决

    I posted the solution here for reference

    更新 - 工作代码:

    为了安全令牌生成,我使用slushie's php implementation

    PHP部分:

    <?PHP 
    
    use ReCaptchaSecureToken\ReCaptchaToken as ReCaptchaToken;
    require_once("libs/ReCaptchaToken.php");
    
    //Generate recaptcha token
    $config = [ 'site_key'      => 'place-your-site-key-here', 
                'site_secret'   => 'place-your-secret-key-here'
                ];
    $recaptcha_token = new ReCaptchaToken($config);
    $recaptcha_session_id = uniqid('recaptcha');
    $recaptcha_secure_token = $recaptcha_token->secureToken($recaptcha_session_id);
    
    ?>
    

    HTML:

    <html>
      <head>
      ...
        <script src='//www.google.com/recaptcha/api.js'></script>
      </head>
      <body>
        <form>
        ...
        <div class="g-recaptcha" data-sitekey="place-your-site-key-here" data-stoken="<?PHP echo $recaptcha_secure_token; ?>"></div>
        </form>
      </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2011-03-15
      • 2022-10-21
      • 1970-01-01
      • 2011-02-15
      • 2015-03-19
      • 2018-01-02
      • 2017-07-22
      • 2016-12-30
      相关资源
      最近更新 更多