Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Session Handling in
Initializing a Session
• Sessions will typically run globally with each page load, so the
session class must either be initialized in
your controller constructors, or it can be auto-loaded by the
system. For the most part the session class will run
unattended in the background, so simply initializing the class
will cause it to read, create, and update sessions.
• To initialize the Session class manually in your controller
constructor, use the $this->load->library function:
• $this->load->library('session');Once loaded, the Sessions
library object will be available using: $this->session
How do ‘Sessions’ work?
• When a page is loaded, the session class will check to see if
valid session data exists in the user's session cookie. If
sessions data does not exist (or if it has expired) a new
session will be created and saved in the cookie. If a session
does exist, its information will be updated and the cookie
will be updated. With each update, the session_id will be
• It's important for you to understand that once initialized,
the Session class runs automatically. There is nothing you
need to do to cause the above behavior to happen. You
can, as you'll see below, work with session data or even add
your own data to a user's session, but the process of
reading, writing, and updating a session is automatic.
What is Session Data?
• A session, as far as CodeIgniter is concerned, is
simply an array containing the following
• The user's unique Session ID (this is a statistically
random string with very strong entropy, hashed
with MD5 for portability, and regenerated (by
default) every five minutes)
• The user's IP Address
• The user's User Agent data (the first 120
characters of the browser data string)
• The "last activity" time stamp.
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');
Adding Custom Session Data
• A useful aspect of the session array is that you can add your
own data to it and it will be stored in the user's cookie.
• this->session->set_userdata($array);Where $array is an
associative array containing your new data. Here's an
• $newdata = array(
'username' => 'johndoe',
'email' => 'firstname.lastname@example.org',
'logged_in' => TRUE
Retrieving All Session Data
• An array of all userdata can be retrieved as
• $this->session->all_userdata()And returns an
associative array like the following:
• Array ( [session_id] =>
[ip_address] => 127.0.0.1 [user_agent] =>
Mozilla/5.0 (Macintosh; U; Intel Mac OS X
10_6_7; [last_activity] => 1303142623 )
Removing Session Data
Just as set_userdata() can be used to add
information into a session, unset_userdata() can
be used to remove it, by passing the session
key. For example, if you wanted to remove
'some_name' from your session information:
• This function can also be passed an associative array of
items to unset.
• $array_items = array('username' => '', 'email' => '');
• To clear the current session:
• if you only want some items destroyed and not all,
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
www.baabtra.com | www.massbaab.com |www.baabte.com
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Ph: + 91 – 495 40 25 550
Start up Village
NC Complex, Near Bus Stand
Ph: + 91 – 495 40 25 550
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.