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