In Set and Display Session Data in CodeIgniter, first we have to learn a little bit about Session in CodeIgniter.

When we develop a website, we need to track user’s state and activity for this we have to use Session. In CodeIgniter session library or class already available to use. We just initialize it on our program.

Set and Display Session Data in CodeIgniter
Set and Display Session Data in CodeIgniter

CodeIgniter Session Initialization

Session in CI, session data are globally available through the complete website, if we want to get those data we have to first initialize the session first.

Set Session Data in CodeIgniter

After initialize the session we can set the user’s data. Here are 2 types of setting methods,

  • Set Single Data in CodeIgniter Session
  • Set Multiple Data in CodeIgniter Session

Set Single Data in CodeIgniter Session

For setting one value we use the $this->session session object and set_userdata function which accepts 2 parameters.

Set Multiple Data in CodeIgniter Session

For setting up the multiple data in session we use the same set_userdata function which can also support array values.

Display Session Data in CodeIgniter

Here are 2 types of displaying session data in CI,

  • Show specific data from the session.
  • Show all data from the session.

Show Specific Data From CodeIgniter Session

For showing specific data we use the key name of the session data. Here is the complete example to Display Session Data in CodeIgniter,

Example:

Show All Data From Session

For showing complete data from the session we use all_userdata() function to get complete data in array format.

Example:

Here are the complete solution to set and display session data in CodeIgniter.

If you want to know more about the session you can go to the official documentation of CodeIgniter, https://codeigniter.com/userguide3/libraries/sessions.html

Also Check:

Happy Coding..!

Was this article helpful?
YesNo

By Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

2 thoughts on “Set and Display Session Data in CodeIgniter”
  1. Hi I have a query,
    I am doing a Ajax call which direct to PHP class and sets userData there and after that is return the data to js , if data is success it redirects to other controller where session i.e $this->session->userdata gets destroyed.
    what could be the reason behind this ?

    thanks

Leave a Reply

Your email address will not be published. Required fields are marked *