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

如何在Codeigniter PHP中创建登录API及接口访问无返回问题排查

问题原因

  • 控制器参数接收逻辑不匹配:CodeIgniter 框架中控制器方法的形参仅能接收URL路径分段传递的参数,对应访问格式为Webservice/login/abc@yahoo.com/123456,你使用Query String(?email=xxx&password=xxx)格式传参时,方法形参无法获取到传入的参数值。
  • 硬编码覆盖了传入参数:你在控制器login方法内部直接写死了$email='abc@yahoo.com'、$password='123456',就算参数传递正确也会被固定值覆盖,逻辑完全错误。

修复方案

修改控制器login方法代码,改为通过Input类获取Query String参数,同时删除硬编码的赋值语句,修复后代码如下:

public function login()
{
    // 移除形参,通过Input类获取GET参数,第二个参数开启XSS过滤
    $email = $this->input->get('email', true); 
    $password = $this->input->get('password', true);
    
    // 增加参数非空校验
    if (empty($email) || empty($password)) {
        $json = [
            'result' => [],
            'status' => 0,
            'msg' => '邮箱和密码不能为空'
        ];
        $this->output
             ->set_content_type('application/json')
             ->set_output(json_encode($json));
        return;
    }

    $result = $this->Webservice_model->login($email, $password);
    $data[] = $result;
    if ($result) {
        $json = [
            'result' => $data,
            'status' => 1,
            'msg' => 'Login Successfully!'
        ];
    } else {
        $json = [
            'result' => $data,
            'status' => 0,
            'msg' => 'Login Failed!'
        ];
    }
    // 用CI自带的Output类输出更规范,避免header手动设置冲突
    $this->output
         ->set_content_type('application/json')
         ->set_output(json_encode($json));
}

如果你希望保持原有路径分段传参的逻辑,只需要删除控制器方法内部硬编码的$email、$password赋值语句即可,对应访问路径改为Webservice/login/abc@yahoo.com/123456就能正常调用。

额外优化建议

  • 当前密码是明文查询存储,建议后续改为bcrypt等哈希加密存储,验证时对传入密码做哈希校验,避免数据泄露风险。
  • 模型查询可以增加limit(1)限制,提升查询效率。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 10:54:03