You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否在不使用Composer的Yii框架项目中安装reCAPTCHA?

不使用Composer在Yii项目中实现reCAPTCHA验证

完全可以不借助Composer实现reCAPTCHA验证,核心是直接调用Google官方的reCAPTCHA API,手动完成前端渲染和后端验证,以下是具体步骤:

1. 获取reCAPTCHA密钥

前往Google reCAPTCHA管理后台注册你的站点,获取site_key(前端使用)和secret_key(后端验证使用)。

2. 前端添加reCAPTCHA验证控件

在需要验证的表单页面中,引入官方JS脚本并渲染验证框:

<!-- 引入reCAPTCHA脚本 -->
<script src="https://www.google.com/recaptcha/api.js" async defer></script>

<!-- 在表单内添加验证区域 -->
<div class="g-recaptcha" data-sitekey="你的site_key"></div>

3. 后端实现验证逻辑

在处理表单提交的Yii控制器方法中,编写代码验证用户的reCAPTCHA响应:

Yii2示例代码

public function actionSubmitForm()
{
    if (Yii::$app->request->isPost) {
        $recaptchaResponse = Yii::$app->request->post('g-recaptcha-response');
        $secretKey = '你的secret_key';
        $verifyEndpoint = 'https://www.google.com/recaptcha/api/siteverify';

        // 构建验证请求
        $postData = http_build_query([
            'secret' => $secretKey,
            'response' => $recaptchaResponse,
            'remoteip' => Yii::$app->request->userIP
        ]);

        // 发送POST请求验证
        $ch = curl_init($verifyEndpoint);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $verifyResult = json_decode(curl_exec($ch), true);
        curl_close($ch);

        if ($verifyResult['success']) {
            // 验证通过,执行表单业务逻辑
            Yii::$app->session->setFlash('success', '验证通过,表单已提交!');
        } else {
            // 验证失败,返回错误提示
            Yii::$app->session->setFlash('error', 'reCAPTCHA验证失败,请重新尝试!');
        }
    }

    return $this->render('form');
}

Yii1示例代码

逻辑与Yii2一致,仅请求参数获取方式不同:

public function actionSubmitForm()
{
    if (Yii::app()->request->isPostRequest) {
        $recaptchaResponse = Yii::app()->request->getPost('g-recaptcha-response');
        $secretKey = '你的secret_key';
        $verifyEndpoint = 'https://www.google.com/recaptcha/api/siteverify';

        $postData = http_build_query([
            'secret' => $secretKey,
            'response' => $recaptchaResponse,
            'remoteip' => Yii::app()->request->userHostAddress
        ]);

        $ch = curl_init($verifyEndpoint);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $verifyResult = json_decode(curl_exec($ch), true);
        curl_close($ch);

        if ($verifyResult['success']) {
            Yii::app()->user->setFlash('success', '验证通过!');
        } else {
            Yii::app()->user->setFlash('error', 'reCAPTCHA验证失败,请重试!');
        }
    }

    $this->render('form');
}

4. 补充说明

  • 如果服务器未安装curl扩展,可替换为file_get_contents(需开启allow_url_fopen配置):
    $verifyResult = json_decode(file_get_contents($verifyEndpoint . '?' . $postData), true);
    
  • 可将验证逻辑封装为Helper类或Yii组件,提升代码复用性
  • 需处理请求超时、网络异常等情况,避免影响表单流程

内容的提问来源于stack exchange,提问作者Adam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 01:37:00