Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php reCAPTCHA v3验证分数有问题_Php_Recaptcha V3 - Fatal编程技术网

Php reCAPTCHA v3验证分数有问题

Php reCAPTCHA v3验证分数有问题,php,recaptcha-v3,Php,Recaptcha V3,试图在我的网站联系人表单上实现recaptcha,我很难通过任何操作,除非我将分数设置为0.0。即使是0.1也会将其转化为垃圾邮件。关于如何实现的例子太多了,我已经尝试了其中的几个,但没有任何运气(因为有几个也适用于不同的版本,这使我们这些noobs很难做到) 在任何情况下,下面是我尝试使用的表单html页面的精简版本: <head> <script src='https://www.google.com/recaptcha/api.js?render=KEY'><

试图在我的网站联系人表单上实现recaptcha,我很难通过任何操作,除非我将分数设置为0.0。即使是0.1也会将其转化为垃圾邮件。关于如何实现的例子太多了,我已经尝试了其中的几个,但没有任何运气(因为有几个也适用于不同的版本,这使我们这些noobs很难做到)

在任何情况下,下面是我尝试使用的表单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>

名称
电子邮件
消息
$(函数(){//等待文档准备就绪
grecaptcha.ready(函数(){
执行('KEY',{action:'contactUs'})。然后(函数(令牌){
//验证服务器上的令牌。
});
});
});
然后我有一个名为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();
    }
}
?>
<?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.0,这是目前电子邮件通过的唯一方式。但当然,这允许通过垃圾邮件或真实的消息,因为它基本上是关闭的。正如我所说,如果我将其设置为0.1,它就不会通过分数检查,也不会发送电子邮件。我确信我遗漏了一些简单的东西,或者我没有正确地传递信息,或者其他什么,但是谷歌文档并不是很有帮助。所以我希望有人能指出我错过了什么

谢谢

终于找到了一个答案,这正是我想要的。一些简单的例子代码,工作!(为什么谷歌不能这么做?)它没有被列为“被接受”的答案,它是下面的答案,但被接受的答案只是把你扔向一个git,这对noobs来说是可笑的困惑

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

<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>

名称
电子邮件
消息
$(函数(){//等待文档准备就绪
grecaptcha.ready(函数(){
执行('YOUR_KEY_HERE',{action:'contactUs'})。然后(函数(令牌){
//验证服务器上的令牌。
document.getElementById('g-recaptcha-response')。value=token;
});
});
});
然后是修改后的PHP表单send_form_email.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();
    }
}
?>
<?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分,但是你当然应该在谷歌上查看你的管理员,看看你得到了什么分数,并根据需要进行调整