为什么我不能在CodeIgniter钩子中使用会话

时间:2013-05-12 22:43:43

标签: php codeigniter session hook

我这里有这个代码: 钩/ account.php:

class Account {

    public function checkIfLogged() {
        if(!$this->session->userdata('logged') ){
            $this->load->view('error/not_found');
            exit;
        }
    }
}

我收到此错误:

  

未定义的属性:Account :: $ session

我可以确认我的钩子是post_controller_constructor。

有人可以告诉我哪里出错了吗?

感谢。

1 个答案:

答案 0 :(得分:4)

你应该使用:

$this->CI = & get_instance();
if(!$this->CI->session->userdata('logged') ){
        $this->CI->load->view('error/not_found');
        exit;
} 

这只是一个范围问题,这就是你在这里使用CI的原因。

相关问题