谷歌reCaptcha 3-第一次失败后登录时出错



我在我这边实现了谷歌reCaptcha 3。出于某种原因,如果用户键入了错误的详细信息,并且出现了错误,他当然不能尝试再次登录。他收到了reCaptcha消息——";repatcha_error(2(";。

BTW-如果他第一次输入正确,他确实能够连接。

为什么会这样,我该怎么解决?

我的表单和脚本部分

<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)';
}
}

首先将method="post"添加到<表单标记,因为默认情况下表单使用GET方法,并且在PHP中您正在查找POST数据

最新更新