Click to share! ⬇️
Codeigniter comes with a great session class. Sessions in any programming language are what give you the ability to keep track of a user’s state while also tracking their activity. In Codeigniter this session information is stored as a cookie. You also have the option to store this session in a database. That is up to you.

codeigniter sessions

How do I start a session

The great thing about using a framework is that many things are done for you. In the case of php sessions, they will usually run globally on every page load. This means that you must either auto load the library in your configuration, or you can trigger the session class in the constructors of your various application controllers. Once this is all set you can create, update, and read sessions.

You can load this session class in your controller with this piece of code:


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

Now you can start accessing all your session data with this simple syntax

$this->session

The method to sessions madness

If you have loaded this session library the software will check to see if there is a valid session on each page load. For the sake of this discussion we will talk about how this works using a cookie rather than a database. If there is a valid session already, Codeigniter will update the session, and store it in the cookie. On the other hand if there is no session a new session will be generated and stored in a cookie. This session ID gets regenerated on every update.

The session data is stored as a serialized array and would look like the following if it were un serialized:

[array]
(
     'session_id'    => random hash,
     'ip_address'    => 'string - user IP address',
     'user_agent'    => 'string - user agent data',
     'last_activity' => timestamp
)

How can I access session data?

You can use the following code to access any piece of information you may want out of the session


$this->session->userdata('some_data');

Note that some_data is the key or index of the value that you are trying to retrieve. If you are trying to access data that does not exist in the session the userdata function will simply return false.

Add your own session data

This is where the session class begins to become very useful. Oftentimes you are going to want to keep track of specific information about the user. The way to do this is to create an associative array and set that array to a variable. You can then use this set_userdata function to add your custom data to the session. Check out this piece of code that gives an example of how to do this.


$customdata = array(
                   'username'  => 'Rick James',
                   'email'     => 'RickyJ@imrickjames.com',
                   'logged_in' => TRUE
               );

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

Sometimes you may just wanna do a dump of the entire contents of the session instead of passing in keys of the session array to access individual values. In this case you can use the all_userdata function.


$this->session->all_userdata()

How do I unset session data?

There might be times in your application development when you want to unset some values in the session array. In this case you can use the unset_userdata function just like you could use the set_userdata function. You can unset one key value at a time or by passing an associative array of the values to unset.


$this->session->unset_userdata('username');  

$array_items = array('username' => '', 'email' => '');
$this->session->unset_userdata($array_items);

Session Flashdata

Flashdata is pretty cool in that it only persists until there is another server request. I’m sure you have seen various applications or websites give you a success or failure message based on an action you had taken. This is often done with JavaScript on the client side but in the PHP world you are usually dealing with Flashdata.

To add flashdata:


$this->session->set_flashdata('item', 'value');

You can also pass an array to set_flashdata(), in the same manner as set_userdata().

To read a flashdata variable:


$this->session->flashdata('item');

If you find that you need to preserve a flashdata variable through an additional request, you can do so using the keep_flashdata() function.


$this->session->keep_flashdata('item');

Will that about wraps it up for our lesson on session data in Codeigniter.

Thank you for reading What are Codeigniter Sessions – If you found this post helpful, Please do share using the buttons below!

Click to share! ⬇️