reCAPTCHA V2在服务器端验证失败

时间:2020-06-18 14:52:42

标签: javascript php html server recaptcha

我的reCaptcha V2“我不是机器人”在服务器端出现故障。 它似乎在以下方面失败了: post_captcha($_POST['g-recaptcha-response'])

公钥和pvt密钥都是正确的,并且在正确的域上。

我创建reCAPTCHA已有3个小时了,所以也许需要一段时间才能通过系统处理我的域?我不确定。...

这是HTML:

<html>
<head>
  <script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>

<body>

<form action="contact-form.php" method="POST">
  <div class="g-recaptcha" data-sitekey="PUBLIC KEY"></div>
  <br/>
  <input type="submit" value="Submit">
</form>

</body>
</html>

这是PHP:

<?php
// Checks if form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    function post_captcha($user_response) {
        $fields_string = '';
        $fields = array(
            'secret' => 'PVT KEY',
            'response' => $user_response
        );
        foreach($fields as $key=>$value)
        $fields_string .= $key . '=' . $value . '&';
        $fields_string = rtrim($fields_string, '&');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

        $result = curl_exec($ch);
        curl_close($ch);

        return json_decode($result, true);
    }

    // Call the function post_captcha
    $res = post_captcha($_POST['g-recaptcha-response']);

    if (!$res['success']) {
        // What happens when the CAPTCHA wasn't checked
        echo '<p>Please go back and make sure you check the security CAPTCHA box.</p><br>';
    } else {
        echo 'success';
    }
}
?>

任何帮助将不胜感激! 谢谢。

1 个答案:

答案 0 :(得分:0)

好久没有回复了。问题是因为我的网络服务提供商对我订阅的共享服务器有限制,例如无法编辑 php.ini 配置文件。

解决方案:不要使用共享服务器!