php 需要 reCAPTCHA 方面的帮助 - 不断收到不正确的验证码溶胶

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1264703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:48:35  来源:igfitidea点击:

Need help with reCAPTCHA - keep getting incorrect-captcha-sol

phphtmlrecaptcha

提问by Anri?tte Myburgh

I am trying to add a reCAPTCHA to my site, but keep getting incorrect-captcha-solerror when I submit the answer.

我正在尝试向我的网站添加 reCAPTCHA,但incorrect-captcha-sol在我提交答案时不断收到错误消息。

Can anyone tell me if I am correct in doing the following?

谁能告诉我我在做以下事情是否正确?

I have a generic index.php, which includes contact.php. In contact.php I have inserted the following code:

我有一个通用的 index.php,其中包括 contact.php。在contact.php中,我插入了以下代码:

require_once('recaptchalib.php');
$publickey = "XXXX";
$privatekey = "XXXX";

//the response from reCAPTCHA
$resp = null;
//the error code from reCAPTCHA, if any
$error = null;

if ($_POST['submit']) {
    $message = $_POST['message_txt'];
    $name = $_POST['name_txt'];
    $email = $_POST['email_txt'];

$emailBody = $message;
$to = 'xx';
$from = $name.' <'.$email.'>';
$subject = 'XX Website Enquiry';
$headers = 'From: '.$from;  

$resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

if ($resp->is_valid) {
    echo 'captcha correct';
    if (mail($to,$subject,$emailBody,$headers)) {
        //echo 'mail sent';
        $confirmation = 'sent';
    }
    else {
        //echo 'mail not sent';
        $confirmation = 'error';
    }
} else {
    # set the error code so that we can display it. You could also use
    # die ("reCAPTCHA failed"), but using the error message is
    # more user friendly

    $error = $resp->error;

    echo $error;
}

}

}

In my html I inserted the CAPTCHA like this:

在我的 html 中,我像这样插入了验证码:

<form name="contactForm" method="post" action="index.php?id=contact&action=submit#contact">
    <tr><td>Name</td><td><div align="right">
      <input type="text" name="name_txt" class="input">
      </div></td></tr>
    <tr><td>Email</td><td><div align="right">
      <input type="text" name="email_txt" class="input">
    </div></td></tr>
    <tr><td height="10"></td></tr>
    <tr><td colspan="2">Message</td></tr>
    <tr><td colspan="2"><textarea name="message_txt" class="textarea" style="width:200px; height:100px"></textarea></td></tr>
    <tr><td colspan="2"><?php echo recaptcha_get_html($publickey, $error); ?></td></tr>
    <tr><td colspan="2" style="padding-top:10px;"><input type="image" src="images/header_06.gif" name="submit" value="submit"></td></tr>
    </form>

I cannot see that I am doing anything wrong, but would appreciate any constructive criticism.

我看不出我做错了什么,但会感谢任何建设性的批评。

TIA

TIA

回答by Anri?tte Myburgh

I have solved this, it is one of the most unusual things I have come across, my syntax was previously:

我已经解决了这个问题,这是我遇到的最不寻常的事情之一,我的语法以前是:

<table>
<form>
<tr><td></td></tr>
</form>
</table>

I changed it to this:

我把它改成这样:

<form>
<table>
<tr><td></td></tr>
</table>
</form>

Because of this switch, suddenly the recaptcha_response_fieldand recaptcha_challenge_fieldare posting values back to the form.

由于这个切换,突然recaptcha_response_fieldrecaptcha_challenge_field将值发布回表单。

I cannot think why this because all MY form variables got posted back before the switch.

我想不出为什么会这样,因为我的所有表单变量都在切换之前发回了。

Thanks all for the pointers.

谢谢大家指点。

回答by Josh

if you are posting the check twice - eg once from javascript and once via php the second one will fail as the API only allows a solution to return valid once.

如果您将支票张贴两次 - 例如一次来自 javascript,一次通过 php,第二次将失败,因为 API 只允许解决方案返回有效一次。

Hope that helps, Josh

希望有帮助,乔希

回答by den

That's because the form cannot be on the outside of the tr.....it has to be on the outisde of table.....form cannot be inserted into table, it can be inserted into the td though.

那是因为表单不能在 tr 的外面.....它必须在表的外面.....表单不能插入到表中,但它可以插入到 td 中。

回答by esteve

In my case, the error was to set the form without specifying:

在我的情况下,错误是在没有指定的情况下设置表单:

method="POST"

方法=“POST”

Cheers!

干杯!

回答by rok

in root directory from the script set php.ini with this:

在脚本设置 php.ini 的根目录中:

allow_url_fopen = On

allow_url_fopen = 开

回答by TigerTiger

Are you sure you are typing in correct words?

您确定您输入的单词正确吗?

this one is from recaptcha website :

这是来自recaptcha网站:

Line 1       "true" or "false". True if the reCAPTCHA was successful
Line 2  if Line 1 is false, then this string will be an error code. reCAPTCHA can 
    display the error to the user (through the error parameter of api.recaptcha.net/challenge).
     Implementations should not depend on error code names, 
as they may change in the future.


    Example: If your response looks like this:

    false
    **incorrect-captcha-sol**

    ... you can add '&error=incorrect-captcha-sol' to the challenge request URL, 
and the user will get an error code.