问题描述
我正在尝试将google reCAPTCHA 3添加到我的网站。
我有登录表格。如果用户填写了正确的详细信息,便可以登录,但是如果输入了错误的内容却会出错,并且由于某种原因他将无法重试-即使输入正确的信息,也会显示相同的消息(recaptcha_error(2)。)>
那是为什么?
HTML PART
<form id="login-form" action="#" >
<input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response">
<input type="hidden" name="action" value="validate_captcha">
<input type="text" name="email">
<input type="password" name="password">
<button type="submit">SEND</button>
</form>
<script src="https://www.google.com/recaptcha/api.js?render=MY_SITE_KEY"></script>
<script>
grecaptcha.ready(function() {
// do request for recaptcha token
// response is promise with passed token
grecaptcha.execute('MY_SITE_KEY',{action:'validate_captcha'})
.then(function(token) {
// add token value to form
document.getElementById('g-recaptcha-response').value = token;
});
});
</script>
PHP-服务器端
if (isset($_POST['g-recaptcha-response'])) {
$captcha = $_POST['g-recaptcha-response'];
} else {
$captcha = false;
}
if (!$captcha) {
$msg = 'recaptcha_error(1)';
} else {
$secret = 'MY_SERVER_KEY';
$response = file_get_contents(
"https://www.google.com/recaptcha/api/siteverify?secret=" . $secret . "&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']
);
$response = json_decode($response);
if ($response->success === false) {
$msg = 'recaptcha_error(2)';
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)