首页 > 解决方案 > 在这种情况下如何验证 Recaptcha?

问题描述

我的网站同时具有 recaptcha v1 和通用验证方法进行验证,然后我决定将我的 recaptcha v1 升级到 recaptcha v2 所以我将所有文件从 Google ReCaptcha git 放在我的网站上。所以验证码现在显示,但每次我点击提交按钮时,它都会显示无效的验证码。

当前用户注册表单有这个代码来验证recaptcha

if(!$captcha->is_valid()) {
    $_SESSION['error'][] = $language->global->error_message->invalid_captcha;
}

我相信问题的根源在于这部分代码

  /* Custom valid function for both the normal captcha and the recaptcha */

 function is_valid() {

        if($this->recaptcha) {

             $recaptcha = new \ReCaptcha\ReCaptcha($this->recaptcha_private_key);
            $response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);

            return ($response->is_valid);

        } else {

            return ($_POST['captcha'] == $_SESSION['captcha']);

        }
    }

标签: phprecaptcha

解决方案


我找到了解决方案

/* Custom valid function for both the normal captcha and the recaptcha */
function is_valid() {

    if($this->recaptcha) {

        $recaptcha = new \ReCaptcha\ReCaptcha($this->recaptcha_private_key);
        $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);

        return ($resp->isSuccess());

    } else {

        return ($_POST['captcha'] == $_SESSION['captcha']);

    }
}

推荐阅读