Peter Stuart
Peter Stuart

Reputation: 2434

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.

Upvotes: 0

Views: 2004

Answers (1)

dennisbot
dennisbot

Reputation: 948

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.

Upvotes: 4

Related Questions