gpt4 book ai didi

php - reCAPTCHA v3 验证分数出现问题

转载 作者:行者123 更新时间:2023-12-04 14:21:23 25 4
gpt4 key购买 nike

尝试在我的网站的联系表单上实现重新验证,但我无法通过,除非我将分数设置为 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,它也不会通过分数检查并且永远不会发送电子邮件。我确定我遗漏了一些简单的东西,或者我没有正确传递信息之类的,但是谷歌文档并不是很有帮助。所以我希望有人能指出我错过了什么?

谢谢!

最佳答案

终于找到答案here这正是我想要的。一些简单的示例代码有效! (为什么谷歌不能那样做?)它没有被列为“已接受”的答案,它是在那个下面的答案,但已接受的答案只会把你扔向一个对新手来说非常困惑的 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 上查看您的管理员,看看您得到的分数是多少,并根据需要进行调整。

关于php - reCAPTCHA v3 验证分数出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54408754/

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