Why I can't use sessions in a CodeIgniter hook

怎甘沉沦 提交于 2019-12-10 12:05:59

问题


I have this code here: hooks/account.php:

class Account {

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

and I get this error:

Undefined property: Account::$session

I can confirm that my hook is a post_controller_constructor.

Can somebody tell me where I am going wrong?

Thanks.


回答1:


you should use:

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

It's just a matter of scope that's why you use CI here this way.



来源:https://stackoverflow.com/questions/16512799/why-i-cant-use-sessions-in-a-codeigniter-hook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!