简体   繁体   中英

Google recaptcha and PHP

I am trying to troubleshoot a form using Google recaptcha version 1.0 - when the form gets submitted the g-recaptcha-response is filled which means Google is happy this is real person but the ReCaptchaResponse is empty meaning the form does not submit as I cannot be sure this is a real person.

When the form is submitted this is the value in $_POST:

[g-recaptcha-response] => 03AHJ_Vuvki7OVSl6bZug1NN5WPOK4HUiLmhAFMMl1RmEiwWzn-tIJ8iryAe01Z-RjzaAtfBKP2ITFc2SnrTV3v7CG7YosOspqXuMSuLDgHCqWG7CEdSCxlSUaaB6joNtOQYPTi0vP4n6sfupG0GfnjMkxJPnEfEnNvsCimsvtIkUbHfY18bYzXTarXCqjb_R_EHcnIl0zm5HhpIeJUFaJYqgivz1gRqkR_BZhlKzvCkVQkRw9Zl-RVRgThUFqI_IFjRnL5ZE5eV1VwrEZFx9j8AOq1gkYw6Cjk8wYSRp9Ax816ifnyYAPtV_NIM6qBpM3NfTejlZ0FT0fu7Kd5mqOFllqSaRVOpSdT-W2HrL4EkNBIeA-YrDwPu9-8-lX3WtZY6kvdm_9Cb_8I1tjKoAmu70mXC0yl5i9Rf0NluS_hAGwCIZFDW9V5PtiiYeD9HpiW3h4nZpzBKAC3FaRuQZNKfTEMPO0TelIlQ [submit] => )

And this is content of ReCaptchaResponse:

ReCaptchaResponse Object ( [success] => [errorCodes] => )

Here is the code I am using to validate the response - it always fails on !$response->success which makes sense as success is empty but why is it empty when g-recaptcha-response is populated?

    $secret= "MY_SECRET";

    require_once('recaptchalib.php');

    $reCaptcha = new ReCaptcha($secret);

    if ($_POST["g-recaptcha-response"]) {
        $response = $reCaptcha->verifyResponse(
            $_SERVER["REMOTE_ADDR"],
            $_POST["g-recaptcha-response"]
        );
        print_r($response);
        print $response->success;
        print $response->error-codes;

        if ($response == null) {
            $error_message='<p>Please validate your captcha response 1</p>';
            $error=1;
        } elseif (!$response->success){
            $error_message='<p>Please validate your captcha response 2</p>';
            $error=1;
        }
    } else {
        $error_message='<p>Please validate your captcha response 3</p>';
        $error=1;
    }

For responses like that, use var_dump() instead of print_r() , as the latter prints false and NULL as whitespace.

print_r($response);
-> ReCaptchaResponse Object
(
    [success] =>
    [errorCodes] =>
)

var_dump($response);
-> object(ReCaptchaResponse)#1 (2) {
  ["success"]=>
  bool(false)
  ["errorCodes"]=>
  NULL
} 

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