【问题标题】:reCAPTCHA V2 Failing On Server Side ValidationreCAPTCHA V2 在服务器端验证失败
【发布时间】:2020-06-18 14:52:42
【问题描述】:

我的 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';
    }
}
?>

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

【问题讨论】:

  • 当你说“失败”时,到底发生了什么? PHP 是否返回任何类型的错误?
  • @ryanwebjackson 完成验证码并提交后,我被带到 .php 文件中,其中显示“请返回并确保检查安全验证码框”。 IE。 !$res['success']

标签: javascript php html server recaptcha


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 2015-03-02
    • 2013-12-10
    • 2019-04-21
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多