我正在尝试向我的网站添加reCAPTCHA,但在提交答案时不断出现incorrect-captcha-sol
错误。
任何人都可以告诉我,我是否正确地执行以下操作?
我有一个通用的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;
}
}
在我的html中,我插入了CAPTCHA:
<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>
我看不出我做错了什么,但会欣赏任何建设性的批评。
TIA
答案 0 :(得分:19)
我已经解决了这个问题,这是我遇到过的最不寻常的事情之一,我之前的语法是:
<table>
<form>
<tr><td></td></tr>
</form>
</table>
我把它更改为:
<form>
<table>
<tr><td></td></tr>
</table>
</form>
由于此次切换,recaptcha_response_field
和recaptcha_challenge_field
突然将值发布回表单。
我无法想到为什么会这样,因为所有的MY表单变量在切换之前都被回发了。
感谢大家指点。
答案 1 :(得分:9)
如果你发布两次支票 - 例如一次来自javascript,一次来自php,第二次会失败,因为API只允许解决方案返回有效一次。
希望有所帮助, 约什
答案 2 :(得分:5)
那是因为表格不能在tr的外面.....它必须在表格的外面.....表格不能插入表格,它可以插入到td中。
答案 3 :(得分:2)
就我而言,错误是设置表单而不指定:
method =“POST”
干杯!
答案 4 :(得分:0)
您确定要输入正确的字词吗?
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.
答案 5 :(得分:0)
在脚本目录中设置php.ini的根目录:
allow_url_fopen =开启
答案 6 :(得分:0)