weird reCaptcha error in PHP

2019-08-15 04:09发布

问题:

I am having a problem with implementing a reCaptcha on my website.

I followed the tutorial here: http://code.google.com/apis/recaptcha/docs/php.html and implemented just a basic reCaptcha with an error message.

Below is some custom code I use in the file which the form is submitted to:

    if (!$resp->isValid) {
        $_SESSION['badLoginCount'] += 1;
        $_SESSION['incorrect-captcha'] = true;
        $_SESSION['incorrect-captcha-error'] = $resp->error;
        header ('Location: ../../signin.php');
        exit;
    }

If the user types the incorrect reCaptcha, the page redirects and an error is shown as expected. However when a user enters the correct reCaptcha, isValid still evaluates to FALSE and runs this branch, however $resp->error contains nothing, and this has made it almost impossible to debug.

Has anyone come across this before? I can't find anything online.

回答1:

A quick and hacky workaround would be to check if $resp->error is empty instead.

if (!empty($resp->error)) {
    $_SESSION['badLoginCount'] += 1;
    $_SESSION['incorrect-captcha'] = true;
    $_SESSION['incorrect-captcha-error'] = $resp->error;
    header ('Location: ../../signin.php');
    exit;
}