Home » Set and Display Session Data in CodeIgniter

Set and Display Session Data in CodeIgniter

  • by

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..!

Subscribe For Latest Updates

Sign up to best of PHP Code Solutions, informed analysis and opinions on what matters to you.
Invalid email address
We promise not to spam you. You can unsubscribe at any time.

Leave a Reply

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