Tutorial - 5 | Session Data | CodeIgniter PHP Framework
Hi guys this tutorial based on PHP Framework. Today in this tutorial we discuss on how to set and get Session in CI.
We have already set project in previous tutorial.
Lets start...
- Go to application ->Home // open home controller.
- First of all load session library in constructor.
function __construct()
{
parent::__construct();
$this->load->model('HomeModel');
$this->load->library('session');
}
public function setsession(){
$newdata = array(
'username' => 'Amit Chanchlani',
'email' => 'AmitChanchlani@amit.com',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
echo "Session Variable set succesfully";
}
- In above method, $newdata is array in which we can store our data variable, 'logged_in'=>TRUE is compulsory using Login program.
- set_userdata() CI function use to set variable in session.
- Now write code for get session variable values. create method as below..
public function getsession(){
$sessionData = $this->session->userdata();
echo "
"; print_r($sessionData); die;
}
- Now Click on Get Session Value, browser show some thing like image below..
- In above line of code show array of stored values in session.
- Now how to delete session from server.
- You can remove session data by 2 methods as below...
$this->session->unset_userdata('some_name');
or
$sessionData = array('username', 'email');
$this->session->unset_userdata($sessionData );
public function removesessiondata(){
$sessionData = array('username', 'email');
$this->session->unset_userdata($sessionData );
echo "Session data removed succesfully.";
}
- After removing session data... some show like this as follow...
- Now Check Session removed successfully or not by click on Get Session Values
- Look at above image that means session variable username and name removed successfully from session.
I hope you like this tutorial.
On next tutorial we will discuss on how to Create Login,, profile and logout pages in CodeIgniter using session data.
Thanks for reading, for more details go through
MyInboxHub - The Tutorial Points

0 comments