【问题标题】:Using Google reCaptcha always shows 'incorrect-captcha-sol'使用 Google reCaptcha 总是显示 'incorrect-captcha-sol'
【发布时间】:2015-05-25 12:28:59
【问题描述】:

我正在尝试在我的网站上使用 Google reCaptcha,在学习了几个教程之后,我在我的 HTML 页面上使用如下:

<section class="row" id="Contact">
        <div class="col-full">
             <form method="post" action="contact.php">
                <input name="sender" placeholder="Name" size="30"><br><br>
                <input name="senderEmail" placeholder="Email" size="30"><br><br>
                <textarea rows="10" cols="30" name="message" placeholder="Message"></textarea><br><br>
                <div class="g-recaptcha" data-sitekey="6LdFOQcTAAAAABKsku3bD6Mp3HuGYNDRN1tLlSkw"></div><br>
                <input type="submit" name="submit">
            </form>
        </div>
</section>

&lt;head&gt; 标签内添加以下内容:

    <script src='https://www.google.com/recaptcha/api.js'></script>

该表单会加载以下 PHP 脚本,如 Google's tutorial 所示:

<?php

require_once('recaptchalib.php');

echo "<pre> _POST: =========\n";
print_r($_POST);
echo "\n=========\n</pre>";

$privatekey = "privatekey";
$resp = recaptcha_check_answer ($privatekey,
                             $_SERVER["REMOTE_ADDR"],
                             $_POST["recaptcha_challenge_field"],
                             $_POST["recaptcha_response_field"]);
if (!$resp->is_valid)
{
  die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
    "(reCAPTCHA said: " . $resp->error . ")");
}

?>

问题是我总是收到错误incorrect-captcha-sol 并且不明白为什么。我从 PHP 脚本得到的输出是:

 _POST: =========
Array
(
 [g-recaptcha-response] => 03AHJ_VusLbTZVbhMBH2-cyOdbM81IoG7ShDtTmyVa8e34PbghyKaRYnrb4bbeuPoJXFa-2sD5B-UIOWq_zClya-VOtq8iLHXBkAR0PrElQYiQjKTm2POkjci43-yEaenh1tu_M4vKsjJGswKFlVs6IdlWhdVaMdznLBeutgHjiHRBy7fzAi0It0x5IadwMxAqVpmG7F1gRvUcQmakij4ObTqPUK4lPdc84HkkKmY62uJ45d8DwKSeR2ifRg6wL9JFxpcZkjSp4oIlltin2uCMiBDU58QQQLLKPJuSKcmm-N5p7OEt7E6gc4UZyTLpKrJ-9mMUusvLpixwFHop0e5155vEZssxzu6Zizo-LyB1mX3XOrQ8LEceydssgLvl9gJBC4f9zDBKPQdLmjfxVKdPCXLtUZVNbMDYe7cNL7gsgwJC-hLbZYTh6UV1nUdKnFnkHAACjI7M28hpKWdiyIwcJ5UAzTMPfIllMw
)

=========

为什么我的 POST 中没有 recaptcha_challenge_fieldrecaptcha_response_field 字段,我该如何解决?

【问题讨论】:

标签: php html recaptcha


【解决方案1】:

我用于recaptcha的代码如下:

这在标题中:

<script src='https://www.google.com/recaptcha/api.js'></script>

这就是您希望实际验证码所在的位置:

<div class="g-recaptcha" data-sitekey="PUBLIC_KEY_HERE"></div>

你可以将这个添加到 div 来改变主题:data-theme="theme"

这是 PHP 脚本:

if(isset($_POST['g-recaptcha-response']))
$captcha=$_POST['g-recaptcha-response'];
if(!$captcha){
    //Captcha hasn't been checked
}
$response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRET_KEY_HERE&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
if($response['success'] == false){
    //Captcha incorrect
}
else {
    //Success code here
}

【讨论】:

  • 好吧,$response['success'] 在我尝试时似乎总是错误的。
  • 这很奇怪,它非常适合我。确保您已正确输入您的公钥和私钥。如果这仍然不起作用,请尝试在一个全新的文件中查看结果是否有所不同。
  • 为什么这行得通而官方 GOOGLE 代码却不行?这是一些奇怪的魔法。非常感谢!
【解决方案2】:

据我所知,您在检查答案后没有else 选项。你检查它是否错了,但如果用户做对了,没有什么可以告诉它该怎么做。尝试添加如下内容:

if (!$resp->is_valid) {
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")");
}
else {
    //Code for success here
}

【讨论】:

  • 我只是没有上传其余代码,因为它与问题无关,$resp-&gt;is_valid 总是返回 false,因为我总是收到错误。
  • 你在使用新的复选框recaptcha吗?如果是这样,我可以发布我使用的代码。
  • 我想我使用的是最新版本。
【解决方案3】:

我使用 Google 新验证码

https://github.com/google/recaptcha官方代码

它是独立包。它适用于 php 很棒。

验证码失败后,我使用 jquery 代码重新加载验证码

grecaptcha.reset();

因为 recapctha 响应在验证后过期并且验证码不会重新加载,所以它会一次又一次地发送过期响应,直到您不重新加载页面以获取新的验证码。

验证码

require('/path/to/recaptcha/autoload.php');

$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
if ($resp->isSuccess()) {
} else { $errors = $resp->getErrorCodes();
foreach ($errors as $code) {
echo "<div class=\"alert alert-danger\">$code</div>";
}
echo "<p><strong>Note:</strong> Error code <tt>missing-input-response</tt> may mean the user just didn't complete the reCAPTCHA.</p>";
}

【讨论】:

    猜你喜欢
    • 2021-08-28
    • 2015-12-28
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 2015-09-16
    • 2015-02-04
    相关资源
    最近更新 更多