duangenshi9836 2015-06-03 19:21
浏览 69

Google recaptcha和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;
    }
  • 写回答

1条回答 默认 最新

  • douli1854 2015-06-04 07:04
    关注

    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
    } 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序