在 g-recaptcha-response Google 的 reCaptcha 中获取 Null [英] Getting Null in g-recaptcha-response Google's reCaptcha
问题描述
我正在尝试实施 Google 的 reCaptcha v.2.0,但由于此 reCaptcha 无法正常工作,我在 g-recaptcha-response 中为 null,并且我总是收到 的错误请点击 reCAPTCHA 框.即使我成功提交了验证码.我 var_dump
$_POST['g-recaptcha-response']
并且我得到 null
.请帮我.下面是我的代码.
I am trying to implement Google's reCaptcha v.2.0 but i am getting null in g-recaptcha-response due to this reCaptcha is not working properly and I am always getting the error that Please click on the reCAPTCHA box. even if I successfully submitted the Captcha. I var_dump
the $_POST['g-recaptcha-response']
and I am getting null
. Please help me. Below is my code.
HTML
<head>
<script src="https://www.google.com/recaptcha/api.js"></script>
</head>
<form action="" method="POST">
<div class="g-recaptcha" style="margin-left: 230px; margin-top: 40px;" data-sitekey="MySiteKey"></div>
</form>
PHP
if (isset($_POST['submit'])) {
if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
//your site secret key
$secret = 'My Site Secret Key';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . $secret . '&response=' . $_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if ($responseData->success) {
// My All Logic Here
} else {
$error[] = 'Robot verification failed, please try again.';
}
} else {
$error[] = 'Please click on the reCAPTCHA box.';
}
}
我总是收到错误请点击 reCAPTCHA 框.也成功提交.请帮我.提前致谢.
I am always getting the error Please click on the reCAPTCHA box. on successful submission too. Please help me. Thanks in advance.
注意:-表单之间没有使用table标签.
Note:- There is no table tag used in between the Form.
推荐答案
我遇到了同样的问题.最奇怪的部分是客户端调用 grecaptcha.getResponse()
返回正确响应.出于某种原因,它只是没有设置 g-recaptcha-response.所以我的解决方法是设置一个数据回调函数来填充一个带有响应的隐藏字段,并改用该服务器端.隐藏的输入还有助于简化客户端验证.
例如:
I've run into the same issue. The strangest part is a client-side call to grecaptcha.getResponse()
returns to correct response. For some reason it's just not setting g-recaptcha-response.
So my workaround was to set a data-callback function to populate a hidden field with the response and use that server-side instead. The hidden input also helps with ease of client-side validation.
eg:
<div class="g-recaptcha" data-callback="captcha_onclick" data-sitekey="######"></div>
<input type="hidden" name="recaptcha" id="recaptchaValidator" />
javascript:
function captcha_onclick() {
document.getElementById('recaptchaValidator').value = grecaptcha.getResponse();
}
服务器端:
if(!empty($_POST['recaptcha'])) {
$captcha = $_POST['recaptcha'];
...
}
这篇关于在 g-recaptcha-response Google 的 reCaptcha 中获取 Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!