Codeigniter无法从模型中找到set_userdata()

时间:2014-06-13 14:09:46

标签: php codeigniter session

会话库是自动加载的

我的模特:

class Login_model extends CI_Model{
    function __construct(){
        parent::__construct();
    }

    public function validate(){
        $username = $this->security->xss_clean($this->input->post('username'));
        $password = $this->security->xss_clean($this->input->post('password'));

        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $query = $this->db->get('users');

        if($query->num_rows == 1)
        {
            $row = $query->row();
            $data = array(
                    'id' => $row->id,
                    'first_name' => $row->first_name,
                    'last_name' => $row->last_name,
                    'username' => $row->username,
                    'validated' => true
                    );
            $this->session->set_userdata($data);    // ### line 28 ### 
            return true;
        }
        return false;
    }
}

给出了这个错误:

  

致命错误:调用未定义的方法Session :: set_userdata()in   /var/www/codeIgniterTest/_application/models/login_model.php在线   28

3 个答案:

答案 0 :(得分:1)

您是否已加载Session库?

class Login_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
        $this->load->library('session');
    }

答案 1 :(得分:1)

尝试调试并检查所有包含的文件。

$included_files = get_included_files();

foreach ($included_files as $filename) {
    echo "$filename\n";
}

答案 2 :(得分:0)

我发现了问题。我创建了自己的Session类,但忘了扩展CI_Session。

感谢您的帮助!