gpt4 book ai didi

servlets - Google Recaptcha v3 示例演示

转载 作者:行者123 更新时间:2023-12-03 08:56:27 28 4
gpt4 key购买 nike

到目前为止,我一直在使用 Google Recaptcha v2,但现在我想使用最新版本 (v3) 更新我的 WebApp。

是否有人可以为基本表单添加一个完全可用的 Google Recaptcha v3 示例,因为我找不到任何可用的演示?

我真的很感激。

非常感谢。

PS:我在服务器端使用 Java Servlets,但如果你解释使用 PHP 或其他什么都没有关系。

最佳答案

实现 ReCaptcha v3 的简单代码
基本的JS代码

<script src="https://www.google.com/recaptcha/api.js?render=your reCAPTCHA site key here"></script>
<script>
grecaptcha.ready(function() {
// do request for recaptcha token
// response is promise with passed token
grecaptcha.execute('your reCAPTCHA site key here', {action:'validate_captcha'})
.then(function(token) {
// add token value to form
document.getElementById('g-recaptcha-response').value = token;
});
});
</script>
基本的 HTML 代码
<form id="form_id" method="post" action="your_action.php">
<input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response">
<input type="hidden" name="action" value="validate_captcha">
.... your fields
</form>
基本的PHP代码
if (isset($_POST['g-recaptcha-response'])) {
$captcha = $_POST['g-recaptcha-response'];
} else {
$captcha = false;
}

if (!$captcha) {
//Do something with error
} else {
$secret = 'Your secret key here';
$response = file_get_contents(
"https://www.google.com/recaptcha/api/siteverify?secret=" . $secret . "&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']
);
// use json_decode to extract json response
$response = json_decode($response);

if ($response->success === false) {
//Do something with error
}
}

//... The Captcha is valid you can continue with the rest of your code
//... Add code to filter access using $response . score
if ($response->success==true && $response->score <= 0.5) {
//Do something to denied access
}
您必须使用 $response.score 的值过滤访问。它可以采用 0.0 到 1.0 之间的值,其中 1.0 表示用户与您的站点的最佳交互,0.0 表示最差的交互(如机器人)。您可以在 ReCaptcha documentation 中看到一些使用示例。 .

关于servlets - Google Recaptcha v3 示例演示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51507695/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com