简体   繁体   中英

reCAPTCHA V2 Failing On Server Side Validation

My reCaptcha V2 "I'm not a robot" is failing on the server side. It seems to be failing at: post_captcha($_POST['g-recaptcha-response'])

Both the public & pvt key are correct and on the correct domain.

It's been 3 hours since I've created the reCAPTCHA so maybe it takes a while to process my domain through the system? I'm not sure....

Here is the 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>

Here is the 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';
    }
}
?>

Any help will be greatly appreciated. Thank you.

Long time no answer. The issue was because of my web service provider having limitations on the shared server I was subscribe to eg not being able to edit php.ini config file.

Solution: don't use a shared server!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM