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

在CodeIgniter 3中检测API请求来自Apache Cordova应用还是浏览器

检测CodeIgniter 3 API请求来源:Cordova应用 vs 浏览器

下面是几种可靠的方式来区分请求是来自Apache Cordova移动应用还是浏览器(移动端/桌面端):

1. 自定义请求头(最推荐)

直接在Cordova的请求中添加一个自定义HTTP头,后端通过读取这个头来识别来源,这种方式可控性强,不易被伪造(若需更高安全性可配合签名验证)。

Cordova端设置请求头(以Fetch为例)

fetch('https://your-api-domain/api/your-endpoint', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-App-Source': 'Cordova' // 自定义标识头
  },
  body: JSON.stringify({ /* 请求数据 */ })
});

CodeIgniter 3后端读取并判断

public function your_endpoint() {
    // 获取自定义请求头,第二个参数TRUE表示过滤XSS
    $app_source = $this->input->get_request_header('X-App-Source', TRUE);
    
    if ($app_source === 'Cordova') {
        // 处理来自Cordova应用的请求
        $this->output
             ->set_content_type('application/json')
             ->set_output(json_encode(['status' => 'ok', 'source' => 'cordova_app']));
    } else {
        // 处理浏览器请求,进一步区分移动端/桌面端
        if ($this->agent->is_mobile()) {
            $this->output
                 ->set_content_type('application/json')
                 ->set_output(json_encode(['status' => 'ok', 'source' => 'mobile_browser']));
        } else {
            $this->output
                 ->set_content_type('application/json')
                 ->set_output(json_encode(['status' => 'ok', 'source' => 'desktop_browser']));
        }
    }
}

2. 解析User-Agent字符串

Cordova的WebView会在请求的User-Agent中携带Cordova标识(比如Android端可能是Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36 Cordova/10.0.0),可以通过解析这个字符串来识别。

CodeIgniter 3后端实现

public function your_endpoint() {
    $user_agent = $this->agent->agent_string();
    
    if (strpos($user_agent, 'Cordova') !== false) {
        // 来自Cordova应用
        $this->output->set_output(json_encode(['source' => 'cordova_app']));
    } else {
        // 浏览器请求,区分移动端/桌面端
        $source = $this->agent->is_mobile() ? 'mobile_browser' : 'desktop_browser';
        $this->output->set_output(json_encode(['source' => $source]));
    }
}

注意:User-Agent可以被手动修改,所以这种方式可靠性不如自定义请求头,适合作为补充方案。

3. 附加请求参数(简易方案)

在Cordova的API请求URL中添加一个专用参数,比如?source=cordova,后端读取该参数判断来源。

Cordova端请求示例

// GET请求
fetch('https://your-api-domain/api/your-endpoint?source=cordova');

// POST请求(参数放在body里)
fetch('https://your-api-domain/api/your-endpoint', {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({
    source: 'cordova',
    // 其他数据
  })
});

CodeIgniter 3后端判断

public function your_endpoint() {
    $source = $this->input->get('source', TRUE) ?? $this->input->post('source', TRUE);
    
    if ($source === 'cordova') {
        // Cordova应用请求
    } else {
        // 浏览器请求,区分移动端/桌面端
    }
}

缺点:参数容易被伪造,仅适合对来源识别精度要求不高的场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 15:02:51