Google reCaptcha - 空错误消息,未收到对问题的回复(未正确输入reCAPTCHA) [英] Google reCaptcha - empty error message, not receiving response to challenge (The reCAPTCHA wasn't entered correctly)
问题描述
直播DEMO
http: //josiahbertoli.com/
我在提交表单时忽略了以下错误(忽略输入字段的空白值)(使用调试代码):
错误
_POST:=========
阵列
(
[first_name] =>
[last_name] =>
[email] => ;
[subject] =>
[comments] =>
[g-recaptcha-response] =>大长价值
)
=========
无法正确输入reCAPTCHA。 (reCAPTCHA说:)
我的网页设置如何:
HTML
< form id =query-formaction =wp-content / themes /portfolio/submit-form.phpmethod =postname =myForm>
< input id =first_namename =first_namesize =35type =textplaceholder =such。John/>
< input id =last_namename =last_namesize =35type =textplaceholder =such。Smith/>
< input id =emailname =emailsize =35type =textplaceholder =such。example@domain.com/>
< input id =subjectname =subjectsize =35type =textplaceholder =such。Feedback/>
< div class =g-recaptchadata-sitekey =6Le8WxcTAAAAAGqymotU9wtOBFEmWgjM3j2kqTcB>< / div>
< input type =submitvalue =Submit/>
< / form>
提交使用POST方法来调用action-form.php,如下所示
PHP
<?php
require_once('recaptchalib.php');
$ privatekey =the-key-that-i-put-in;
echo< pre> _POST:========= \\\
;的print_r($ _ POST);回声\\\
========= \\\
< / pre>;
$ resp = recaptcha_check_answer($ privatekey,$ _SERVER [REMOTE_ADDR],
$ _POST [recaptcha_challenge_field],
$ _POST [recaptcha_response_field]);
$ resp = null;
if(!$ resp-> is_valid){
//当CAPTCHA输入错误时会发生什么情况
die(reCAPTCHA输入错误,请返回并重试。(reCAPTCHA表示:。$ resp->错误。)); }
else {
//你的代码在这里处理成功的验证
包括'../../../../process.php';
}
?>
感谢您的回应,我很感激。
为了显示reCAPTCHA小部件,您不需要任何库文件,只需包含必要的JavaScript资源,如下所示:
< html>
< head>
< title> reCAPTCHA演示:简单网页< /标题>
< script src =https://www.google.com/recaptcha/api.jsasync defer>< / script>
< / head>
< body>
< div class =g-recaptchadata-sitekey =your_site_key>< / div>
< br />
< input type =submitvalue =提交>
< / form>
< / body>
< / html>
以下是参考资料:
>
现在转到用户的回复。由于您使用的是Google reCAPTCHA V2,因此您应该使用POST参数 g-recaptcha-response 获取用户的回复。 这里是参考:
所以你的代码应该是这样的:
$ b $ ($ set($ _ POST ['g-recaptcha-response'])&&!empty($ _ POST ['g- recaptcha-response'])){
$ privatekey =YOUR_PRIVATE_KEY;
//获取验证回复数据
$ param =https://www.google.com/recaptcha/api/siteverify?secret=\".$privatekey.\"&response= $ _ POST [ 'G-验证码 - 响应']。
$ verifyResponse = file_get_contents($ param);
$ responseData = json_decode($ verifyResponse);
if($ responseData-> success){
// success
echosuccess;
}其他{
//失败
回声失败;
} else {
//用户未输入reCAPTCHA
echoreCAPTCHA输入错误,请返回并重试。
}
?>
EDIT: I'm using reCaptcha 2.0, should I not be using recaptchalib.php?
Live DEMO http://josiahbertoli.com/
I'm getting the following error when I submit a form (ignore the blank values for the input fields)(with debug code):
Error
_POST: =========
Array
(
[first_name] =>
[last_name] =>
[email] =>
[subject] =>
[comments] =>
[g-recaptcha-response] => big-long-value
)
=========
The reCAPTCHA wasn't entered correctly. Go back and try it again.(reCAPTCHA said: )`
How my webpage is set up:
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 uses method POST to call the action submit-form.php which is as follows
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';
}
?>
Thank you for your responses, I appreciate it.
For displaying the reCAPTCHA widget you don't need any library file, you just have to include the necessary JavaScript resource, like this:
<html>
<head>
<title>reCAPTCHA demo: Simple page</title>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<form action="?" method="POST">
<div class="g-recaptcha" data-sitekey="your_site_key"></div>
<br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
Here's the reference:
Now comes to your user's response. Since you're using Google reCAPTCHA V2, you should fetch the user's response using the POST parameter g-recaptcha-response.
Here's the reference:
So your code should be like this:
<?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.";
}
?>
这篇关于Google reCaptcha - 空错误消息,未收到对问题的回复(未正确输入reCAPTCHA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!