简体   繁体   中英

Why I can't use sessions in a CodeIgniter hook

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM