Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube

Tuesday 11 March 2014

How To Create Session In CodeIgniter

1. What is Session?

Session maintains a user's state and tracks user's activity while user browse the website. We have to maintain session manually because HTTP is a stateless protocol.

How To Create Session
Session Creation In CodeIgniter


2. Session Initialization

To avail session in all pages of website then first we have to initialize Session class.


We can initialize Session class as below:

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

After above statement, now we can access session library using '$this->session'.

Created session saved in the cookie.

3. What Session Data Contain?

In CodeIgniter, session is just an array and contains following information:
  • User's unique session id (Hashed with MD5)
  • User's IP address
  • User's User Agent data
  • 'last activity' time stamp
For example
[array]
(
    'session_id' => random hash,
    'ip_address' => 'user IP address',
    'user_agent' => 'user agent data',
    'last_activity' => timestamp
)

Above data is available by default.

4. How To Add Custom Data To Session?

We can store our own data in the user's cookie. There are two ways to add data to the session, listed below:
  • Using array
  • One value at a time
Let's see one example of using array:

$userdata=array(
    'username' => 'xyzname',
    'emailid' => 'xyz@abc.com'
);
$this->session->set_userdata($userdata);

Let's see one example of one value at a time:

$this->session->set_userdata('username','xyzname');
$this->session->set_userdata('emailid','xyz@abc.com');

Cookie can only hold 4KB of data, so careful about data size.

5. How To Retrieve/Get Session Data?

Session data can be retrieved using all_userdata() or userdata() functions.

$this->session->all_userdata();

$this->session->userdata('session-key-name');

all_userdata() function returns an associative array as follows:

Array
(
   [session_id] => d1f7ddfa2c71b9aba777763c74047c3c
   [ip_address] => '127.0.0.1'
   [user_agent] => Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0
   [last_activity] => 1394467693
)

To extract individual session then use userdata() function as described below:

$this->session->userdata('user_name')

In above example, session key is 'user_name'.

6. How To Remove/Unset Session Data?

To remove/unset session data unset_userdata() function is used. Provide session key to unset_userdata() function.

If we want to remove/unset particular session then provide that session key.

For example:
$this->session->unset_userdata('username');
$this->session->unset_userdata('emailid');

If we want to remove/unset session using associative array then consider following example:

$array_data = array('username'=>'','emailid'=>'');
$this->session->unset_userdata($array_data);

7. How To Destroy Session?

If we want to destroy current session then use following function:

$this->session->sess_destroy();

This function will destroy current session.

8. About Flashdata

Flashdata is useful when we want to display informational or status messages like 'your data is saved', 'data is retrieving', '5 record deleted', etc.

Flashdata is only available to the next server request then after automatically cleared.

How To Add Flash Data

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

We can also pass an array to the function same as set_userdata().

Retrieve/Read Flash Data

To retrieve flashdata we need flashdata() function as described below:

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

We can preserve flashdata through an additional request using keep_flashdata() function.

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

In CodeIgniter, session management is very easy. If you find any difficulty then comment below.

1 comments :