reCAPTCHA v3 验证分数出现问题

Trouble with reCAPTCHA v3 verify score

尝试在我的网站的联系表单上实施重新验证,但我无法通过任何事情,除非我将分数设置为 0.0。即使是 0.1 也会将其转为垃圾邮件。关于如何实现的例子太多了,我已经尝试了其中的几个但没有任何运气(因为几个也是针对不同版本的,这让我们菜鸟很难)。

无论如何,这是我尝试使用的 html 页面形式的精简版:

<head>
<script src='https://www.google.com/recaptcha/api.js?render=KEY'></script>
</head>
<body>
<form name="contactform" action="send_form_email.php" method="post">
<div class="input-group">
    <span class="input-group-label">Name</span>
    <input name="realname" class="input-group-field" type="text" value="Your Name Here" maxlength="50" onFocus="this.value=''">
</div>
<div class="input-group">
    <span class="input-group-label">Email</span>
    <input name="email" class="input-group-field" type="email" value="Your E-Mail Here" maxlength="50" onFocus="this.value=''">
</div>
<div class="input-group">
    <span class="input-group-label">Message</span>
    <textarea name="message" rows="10"></textarea>
</div>          
<input type="Submit" class="button" value="SEND"><input type="Reset" class="button" value="RESET">
</form>
<script>
    $(function(){ //wait for document ready
        grecaptcha.ready(function() {
            grecaptcha.execute('KEY', {action: 'contactUs'}).then(function(token) {
            // Verify the token on the server.
            });
        });
    });
</script>
</body>

所以我有一个名为 send_form_email.php 的 PHP 表格,我用它来处理所有艰苦的工作:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Build POST request:
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = 'SECRET_KEY';
$recaptcha_response = $_POST['g-recaptcha-response'];
// Make and decode POST request:
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
$recaptcha = json_decode($recaptcha);

    // Take action based on the score returned:
    if ($recaptcha->score >= 0.0) {
    // This is just where I take care of formatting the email and sending it to me, which is working just fine... well while the score is set to 0.0
    }
    } else {
    // otherwise, let the spammer think that they got their message through
    header('Location: success.htm');
    exit();
    }
}
?>

这就是我 运行 进入我的问题的地方。在上面的代码中,我将它设置为 0.0,这是目前电子邮件通过的唯一方式。但是当然这可以让垃圾邮件或真实消息通过,因为它基本上是关闭的。正如我所说,即使我将它设置为 0.1,它也不会通过分数检查并且永远不会发送电子邮件。我确定这是我遗漏的简单问题,或者我没有正确传递信息之类的,但是 google 文档不是很有帮助。所以我希望有人能指出我错过了什么?

谢谢!

终于找到了一个答案 ,它正是我所寻找的。一些简单的示例代码有效! (为什么 google 不能那样做?)它没有被列为 'accepted' 答案,它是下面那个答案,但接受的答案只是把你扔向 git 那是新手可笑地感到困惑。

这是我从上面编辑的代码:

<head>
<script src='https://www.google.com/recaptcha/api.js?render=YOUR_KEY_HERE'></script>
</head>
<body>
<form name="contactform" action="send_form_email.php" method="post">
<input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response">
<input type="hidden" name="action" value="validate_captcha">
<div class="input-group">
    <span class="input-group-label">Name</span>
    <input name="realname" class="input-group-field" type="text" value="Your Name Here" maxlength="50" onFocus="this.value=''">
</div>
<div class="input-group">
    <span class="input-group-label">Email</span>
    <input name="email" class="input-group-field" type="email" value="Your E-Mail Here" maxlength="50" onFocus="this.value=''">
</div>
<div class="input-group">
    <span class="input-group-label">Message</span>
    <textarea name="message" rows="10"></textarea>
</div>          
<input type="Submit" class="button" value="SEND"><input type="Reset" class="button" value="RESET">
</form>
<script>
    $(function(){ //wait for document ready
        grecaptcha.ready(function() {
            grecaptcha.execute('YOUR_KEY_HERE', {action: 'contactUs'}).then(function(token) {
            // Verify the token on the server.
            document.getElementById('g-recaptcha-response').value = token;
            });
        });
    });
</script>
</body>

然后是名为 send_form_email.php 的修改后的 PHP 表格,我用它来处理所有繁重的工作:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Build POST request:
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = 'YOUR_SECRET_KEY';
$recaptcha_response = $_POST['g-recaptcha-response'];
// Make and decode POST request:
$recaptcha = file_get_contents($recaptcha_url.'?secret='.$recaptcha_secret.'&response='.$recaptcha_response);
$recaptcha = json_decode($recaptcha);

    // Take action based on the score returned:
    if ($recaptcha->score >= 0.5) {
    // Basically if the score is equal to or better than the above, you have a good one and can send your email off and this is just where you would do that
    }
    } else {
    // otherwise, let the spammer think that they got their message through
    header('Location: success.htm');
    exit();
    }
}
?>

我现在显示的是 0.5 分,但你当然应该在 google 上查看你的管理员,看看你得到了什么分数并根据需要进行调整。