Session Class

To initialize the Session class manually in your controller constructor, use the $this->load->library function:

$this->load->library('session');



To add your data to the session array involves passing an array containing your new data to this function:

$this->session->set_userdata($array);
 
Where $array is an associative array containing your new data. Here's an example:

$newdata = array(
                   'username'  => 'johndoe',
                   'email'     => 'johndoe@some-site.com',
                   'logged_in' => TRUE
               );

$this->session->set_userdata($newdata);


If you want to add userdata one value at a time, set_userdata() also supports this syntax.

$this->session->set_userdata('some_name', 'some_value');

Retrieving Session Data

Any piece of information from the session array is available using the following function:

$this->session->userdata('item');
 
Where item is the array index corresponding to the item you wish to fetch. For example, to fetch the session ID you will do this:

$session_id = $this->session->userdata('session_id');
 
Note: The function returns FALSE (boolean) if the item you are trying to access does not exist.