Question: I want to check if user is logged in CodeIgniter by using my library in the controller's constructor.

This is my library:

```
class Administrator_libs {

    public function validate_authen(){
        if( $this->session->userdata('user_authen') ){
            redirect(base_url().'admin/login/');
        }   
    }
}

```

And this is my controller:

```
class Administrator extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->library('administrator_libs');
        $this->administrator_libs->validate_authen();
        $this->load->model('mod_menu');
    }
}

```

But I get this error message:

```
Undefined property: Administrator_libs::$session

```

How can I use session in a library in CodeIgniter?

Answer: 
If you want to access any CodeIgniter library inside of your own, you must call `get_instance()`. This is because `$this` is bound to your current library and not the CodeIgniter object.

```
$CI =& get_instance();
if( $CI->session->userdata('user_authen') ){
    redirect(base_url().'admin/login/');
}

```

Please see [Creating Libraries](http://codeigniter.com/user_guide/general/creating_libraries.html) CodeIgniter Documentation. Specifically the content under **Utilizing CodeIgniter Resources within Your Library**

This assumes you autoload the session library in `config/autoload.php`, if not, you'll also need to add `$CI->load->library("session");` after `$CI` instantiation.

**IMPORTANT:** `=&` is not a typo. It's passed by reference to save memory.
You should simply go to application/autoload.php and add your autoload package which should look like somewhat like this : `$autoload['packages'] = array('database','form_validation','session','email');`

you can see there is session package that i added in my packages. Now coming to your constructor you should load this package by adding this : `$this->load->library("session");`