codeigniter_tutorial
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
13. Session Management<br />
CodeIgniter<br />
When building websites, we often need to track user’s activity and state and for this<br />
purpose, we have to use session. CodeIgniter has session class for this purpose.<br />
Initializing a Session<br />
Sessions data are available globally through the site but to use those data we first need<br />
to initialize the session. We can do that by executing the following line in constructor.<br />
$this->load->library('session');<br />
After loading the session library, you can simply use the session object as shown below.<br />
$this->session<br />
Add Session Data<br />
In PHP, we simply use $_SESSION array to set any data in session as shown below.<br />
$_SESSION[‘key’] = value;<br />
Where ‘key’ is the key of array and value is assigned on right side of equal to sign.<br />
The same thing can be done in CodeIgniter as shown below.<br />
$this->session->set_userdata('some_name', 'some_value');<br />
set_userdata() function takes two arguments. The first argument, some_name, is the<br />
name of the session variable, under which, some_value will be stored.<br />
set_userdata() function also supports another syntax in which you can pass array to<br />
store values as shown below.<br />
$newdata = array(<br />
'username' => 'johndoe',<br />
);<br />
'email'<br />
'logged_in' => TRUE<br />
=> 'johndoe@some-site.com',<br />
$this->session->set_userdata($newdata);<br />
55