php - 在这种情况下如何验证 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']);
}
}
解决方案
我找到了解决方案
/* 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']);
}
}
推荐阅读
- android - 如何在 Flutter 中更改蓝牙设备名称?
- jquery - 如何使用 jquery easyui datagrid 在行中创建组合框
- python - 结合 Pandas 数据框
- amazon-web-services - 将 WorkDocs Sync 安装到 Amazon Workspace
- php - 如何创建与 Sendle API 的 curl 连接
- c++ - 前序遍历(二叉树) - 迭代法
- mongodb - 如何从 mongodb 中的 HH:MM:SS 字符串时间值聚合分钟数据
- javascript - Openweathermap 循环遍历城市 id 以获取当前天气
- templates - 为什么我收到模板类堆栈的分段错误?
- javascript - Javascript:带有数组和超时的承诺函数