A session is simply an array consisting of the following user information:
Source (what-is-session-data)
such as the SessionID:
$this->session->userdata('session_id');
Note - for Codeigniter 3.x, you can use the above syntax, but the concept or magic getters has been introduced, where you can use $this->session->session_id
.
Remember that the userdata()
returns NULL if the session item doesn't exist.
To retrieve all session data
$this->session->all_userdata()
the set_userdata()
method allows you to set data into your session, the following example demonstrates an example array you wish to insert:
$newdata = array(
'username' => 'johndoe',
'email' => '[email protected]',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
You can also set one data at a time, for example:
$this->session->set_userdata('some_name', 'some_value');
or
$some_name = 'some_value';
$this->session->set_userdata($some_name);
$this->session->unset_userdata('some_name')
This method also accepts an array of item keys to unset:
For Codeiginter 3.x:
$array_items = array('username', 'email');
$this->session->unset_userdata($array_items);
For Codeiginter 2.x (this legacy syntax doesn't support 3.x):
$array_items = array('key' => 'value');
$this->session->unset_userdata($array_items);