Php Google reCaptcha-空错误消息,未收到质询响应(未正确输入reCaptcha)
编辑:我正在使用reCaptcha 2.0,我是否应该使用recaptchalib.php 现场演示 当我提交表单时(忽略输入字段的空白值)(调试代码),我得到以下错误: 错误Php Google reCaptcha-空错误消息,未收到质询响应(未正确输入reCaptcha),php,html,recaptcha,Php,Html,Recaptcha,编辑:我正在使用reCaptcha 2.0,我是否应该使用recaptchalib.php 现场演示 当我提交表单时(忽略输入字段的空白值)(调试代码),我得到以下错误: 错误 _POST: ========= Array ( [first_name] => [last_name] => [email] => [subject] => [comments] => [g-recaptcha-response]
_POST: =========
Array
(
[first_name] =>
[last_name] =>
[email] =>
[subject] =>
[comments] =>
[g-recaptcha-response] => big-long-value
)
=========
没有正确输入reCAPTCHA。回去再试一次`
如何设置我的网页:
HTML
<form id="query-form" action="wp-content/themes/portfolio/submit-form.php" method="post" name="myForm">
<input id="first_name" name="first_name" size="35" type="text" placeholder="e.g. John" />
<input id="last_name" name="last_name" size="35" type="text" placeholder="e.g. Smith" />
<input id="email" name="email" size="35" type="text" placeholder="e.g. example@domain.com" />
<input id="subject" name="subject" size="35" type="text" placeholder="e.g. Feedback" />
<textarea id="comments" name="comments"></textarea>
<div class="g-recaptcha" data-sitekey="6Le8WxcTAAAAAGqymotU9wtOBFEmWgjM3j2kqTcB"></div>
<input type="submit" value="Submit" />
</form>
Submit使用方法POST调用操作Submit-form.php,如下所示
PHP
<?php
require_once('recaptchalib.php');
$privatekey = "the-key-that-i-put-in";
echo "<pre> _POST: =========\n"; print_r($_POST); echo "\n=========\n</pre>";
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
$resp = null;
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")"); }
else {
// Your code here to handle a successful verification
include '../../../../process.php';
}
?>
要显示reCAPTCHA小部件,您不需要任何库文件,只需包含必要的JavaScript资源,如下所示:
<?php
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
$privatekey = "YOUR_PRIVATE_KEY";
//get verified response data
$param = "https://www.google.com/recaptcha/api/siteverify?secret=".$privatekey."&response=".$_POST['g-recaptcha-response'];
$verifyResponse = file_get_contents($param);
$responseData = json_decode($verifyResponse);
if($responseData->success){
// success
echo "success";
}else{
// failure
echo "failure";
}
}else{
// user didn't enter reCAPTCHA
echo "The reCAPTCHA wasn't entered correctly. Go back and try it again.";
}
?>
reCAPTCHA演示:简单页面
以下是参考资料:
现在来看看用户的反应。由于您使用的是Google reCAPTCHA V2,因此应该使用POST参数g-reCAPTCHA-response获取用户的响应
以下是参考资料:
因此,您的代码应该如下所示:
<?php
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
$privatekey = "YOUR_PRIVATE_KEY";
//get verified response data
$param = "https://www.google.com/recaptcha/api/siteverify?secret=".$privatekey."&response=".$_POST['g-recaptcha-response'];
$verifyResponse = file_get_contents($param);
$responseData = json_decode($verifyResponse);
if($responseData->success){
// success
echo "success";
}else{
// failure
echo "failure";
}
}else{
// user didn't enter reCAPTCHA
echo "The reCAPTCHA wasn't entered correctly. Go back and try it again.";
}
?>
你好,Rajdeep Paul,谢谢你的回复。我已经得到了你发布的代码,除了当我在没有首先进入recaptcha的情况下单击submit时,else没有启动。寻找yourself@user1395909只需添加一个else
部分即可。我已经更新了代码。@user1395909很高兴我能帮上忙。:-)如果答案解决了您的问题,请将其标记为已接受。GET选项似乎不再有效。以前是这样的。。我想???我使用CURL更新了POST选项,这解决了我的问题