Recaptcha V2返回托管在一个域上而不是其他域的bool(false)?

时间:2018-06-13 16:26:17

标签: php recaptcha

我已经与一家托管公司(1& 1)实施了Recaptcha v2而没有任何问题。我和其他托管公司(XILO)使用了相同的代码框架,Recaptcha总是返回bool(false)。我在本地运行相同的代码(MAMP),它在该环境中也能完美运行。

我已经仔细检查过密钥是否正确,当然还有Google正确列出了域名(包括localhost)。

检查变量$ recaptchaResult,它总是bool(false),因此$ recaptchaResultObject为Null。

这显然是一个环境问题,我也会通过XILO托管提出这个问题。

如果有其他人遇到过这个问题我会感兴趣(即在一个环境中工作,而不是在另一个环境中工作)。

我的PHP代码......

// Recaptcha handling
$recaptchaResponse = $_POST["g-recaptcha-response"];

if(empty($recaptchaResponse)) {

    echo "You must complete the Recaptcha challenge in order to submit your enquiry";
    http_response_code(400);
    exit;

} else {

    # Now post to Recaptcha
    $remoteIp = $_SERVER["REMOTE_ADDR"];

    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $data = array('secret' => $recaptchaSecret, 'response' => $recaptchaResponse, 'remoteip' => $remoteIp);

    // use key 'http' even if you send the request to https://...
    $options = array(
        'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
        ),
    );

    $context = stream_context_create($options);
    $recaptchaResult = file_get_contents($url, false, $context);

    $recaptchaResultObject = json_decode($recaptchaResult);

    // Check hostname is client's domain
    # $thisServer = $_SERVER['SERVER_NAME']; //removed since UseCanonicalName and ServerName settings cannot be guaranteed immutable on shared server

    $thisServer = 'clientdomain.com';

    var_dump($recaptchaResult);


    if(
        ($recaptchaResultObject->hostname == $thisServer ||
         $recaptchaResultObject->hostname == 'www.' + $thisServer)
         && 
         $recaptchaResultObject->success == TRUE
        ) {

    } else {

        // Fatal error
        http_response_code(403);
        echo "There was a problem with your submission, please try again. (Recaptcha)";
        exit;
    }
}

0 个答案:

没有答案