我正在尝试实现 Google 的 reCaptcha v.2.0 但我在中得到 nullg-recaptcha-响应由于此 reCaptcha 无法正常工作,我总是收到以下错误请单击 reCAPTCHA 框。即使我成功提交了验证码。我var_dump
the $_POST['g-recaptcha-response']
我得到了null
。请帮我。下面是我的代码。
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 框。提交成功后也是如此。请帮我。提前致谢。
Note:-没有table表单之间使用的标记。
我遇到了同样的问题。最奇怪的部分是客户端调用grecaptcha.getResponse()
返回正确的响应。由于某种原因它只是没有设置g-recaptcha-响应。
因此,我的解决方法是设置一个数据回调函数,用响应填充隐藏字段并使用该服务器端。隐藏的输入还有助于简化客户端验证。
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'];
...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)