我无法获得简单的 reCaptcha v2 响应

问题描述

我以前使用过 reCaptcha v1,一切都很好,直到几年前 Google 发生变化。我现在被要求更新一个站点,即使是简单的测试代码,我也遇到了一些问题。下面的示例是单个 index.PHP 页面,它在提交表单时捕获“POST”。

出现 reCaptch 勾选框,并允许勾选,但提交表单时,我只收到“检查 reCAPTCHA 框”响应。它看起来像 '$captcha=$_POST['g-recaptcha-response'];'是“空白”。我正在使用正确的代码,并在 reCaptcha 管理页面获取统计信息,但它也在报告:

我们检测到您的网站未验证 reCAPTCHA 解决方案。这是在您的站点上正确使用 reCAPTCHA 所必需的。请访问我们的开发者网站了解更多信息。

我缺少什么需要验证?

<head>

<title>Test Contact Form</title>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
 
</head>
<body>
<?PHP
if (isset($_POST['submit'])) {
    echo "submit detected..<br>"; 
    $full_name;$email;$subject;$message;$captcha;
        if(isset($_POST['full_name'])){
            $full_name=$_POST['full_name'];
        }if(isset($_POST['email'])){
            $email=$_POST['email'];
        }if(isset($_POST['subject'])){
            $subject=$_POST['subject'];
        }if(isset($_POST['message'])){
            $message=$_POST['message'];
        }if(isset($_POST['g-recaptcha-response'])){
            $captcha=$_POST['g-recaptcha-response'];
        }
        if(!$captcha){
            echo 'Check the reCAPTCHA Box.';
            echo $full_name . "<br>";
            echo $email . "<br>";
            echo $subject . "<br>";
            echo $captcha . "<br>";
            exit;
        }
        $secretKey = "6LemhA8aFAKEDRSKVHsecretVzN7Uv1BVQnpS-LwbjOo";
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?".$secretKey."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
        if($response.success==false)
        {
            echo 'You are a robot!';
        }else
        {
            echo "I'd be sending an email to " . $email . "<br>";
            echo "because the secret key" . $secretKey . "<br>";
            echo "and the response " . $captcha . "<br>";
            echo "matched.";
        }
     } ELSE {
?>
    
    
    
    <div class="contact-form">
        <h3>Send me your message v1</h3>
            <form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
            <div class="text-fields">
                <span>Full Name *:</span>
                <input name="full_name" type="text" class="text" value="Your Name">
            </div>
            <div class="text-fields">
                <span>E-mail *:</span>
                <input name="email" type="text" class="text" value="user@domain.com">
            </div>
            <div class="clearfix"> </div>
            </div>
                <div class="subject-field">
                <span>Subject *:</span>
                <input name="subject" type="text" class="text" value="Your Subject">
            </div>
            <div class="message-field">
                <span>Message *:</span>
                <textarea name="message"> </textarea>
            </div>
            <div class="g-recaptcha" data-sitekey="6LemhA8aFAKEJnoerEbOVLWEkeyYO3CcPo-HEIymP"></div>
            <input type="submit" name="submit" value="Send" />
        </form>
    </div>
<?PHP } ?> 
</body>
</html>

解决方法

Captcha v2 返回 JSON 数据(如在官方网站上看到的)您应该解码 json 然后验证它。

    $url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
    'secret' => 'YOUR_SECRET','response' => $_POST["g-recaptcha-response"]
);
$options = array(
    'http' => array (
        'method' => 'POST','content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$verify = file_get_contents($url,false,$context);
$captcha_success=json_decode($verify);

if ($captcha_success->success==false) {
    echo "<p>You are a bot! Go away!</p>";
} else if ($captcha_success->success==true) {
    echo "<p>You are not not a bot!</p>";
}

在“if($response.success==false)”的代码中还有一个问题,你应该把它写成“if($response->success==false)”

我会在核对你的秘密ID后发表评论(后记删除)

,

我怀疑这是因为您在 </div> 之后有一个额外的 <div class="clearfix"> </div> – 如果您删除它,您的代码可能会起作用。

额外的结束 div 标签让浏览器相信您正在关闭 <div class="contact-form"> 并且由于 <form> 标签在其中,它会尝试修复您的 HTML 并关闭它。但是,这意味着剩余的字段(subjectmessage)和 reCAPTCHA 位于 <form> 之外。 reCAPTCHA 仅在 g-recaptcha-response 实际位于 <form> 内时才设置 $response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?".$secretKey."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']),true); if($response["success"]==false) ,无论用户是否成功完成。

额外调整

您还需要像这样解码从 https://www.google.com/recaptcha/api/siteverify 返回的 JSON:

push