Google Recaptcha v3 示例演示 [英] Google Recaptcha v3 example demo

查看:73
本文介绍了Google Recaptcha v3 示例演示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Until now, I was working with Google Recaptcha v2, but now I want to update my WebApp using the lastest version (v3).

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

Is it possible to anyone add a fully working Google Recaptcha v3 example for a basic form as I can't find any working demos of it?

我真的很感激.

非常感谢.

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

PS: I'm using Java Servlets on the server side, but it doesn't matter if you explain using PHP or whatever.

推荐答案

实现 ReCaptcha v3 的简单代码

Simple code to implement 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 文档中看到一些使用示例.

You have to filter access using the value of $response.score. It can takes values from 0.0 to 1.0, where 1.0 means the best user interaction with your site and 0.0 the worst interaction (like a bot). You can see some examples of use in ReCaptcha documentation.

这篇关于Google Recaptcha v3 示例演示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆