Session & Cookies
What is Session?

In Session we save value in session variable and this variable is available to all pages of the application. For this purpose first load session library then save session data in variable, then we can fetch this data in all pages.

Load Session Library

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

How to create session?

$this->session->set_userdata('username', 'shishirphpdeveloper');

where username is session name and shishirphpdeveloper is data for this session.

How to access session?

echo $name = $this->session->userdata('username');

Here we access session(username).

How to delete session?

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

Here we delete session(username).

What is cookie?

Cookie is similar to session, cookie is also save data and available in all page of the application but difference is that
Cookie create txt file and store on client computer with specified time.

Load Cookie Library

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

How to create cookie?

set_cookie('username','shishirphpdeveloper','10'); 

where username is a cookie name,  shishirphpdeveloper is data and 10 sec is time for this cookie avaiable.

How to access cookie?

echo  $name = get_cookie('username');

Here we access cookie(username).

How to delete cookie?

delete_cookie('username'); 

Here we delete cookie(username).


Previous
Next