CodeIgniter Session Management - Mostlikers

08 June, 2015

CodeIgniter Session Management

In this post i have explain about how to store user information in session and retrieve the session information. Codeigniter provide session class The Session class permits you maintain a user's "state" and track their activity while they browse your site.

Session in codeigniter


Here we have use CodeIgniter’s session class that will maintain and help to store session information for each user. The Session class stores session information for each user as serialized (and optionally encrypted) data in a cookie. It can also store the session data in a database table for added security.

Set session in codeigniter


Session will typically run all page or controller so you have to set session in autoload libraries files. it will run all page session value.

Path : application/config/autoload.php
$autoload['libraries'] = array('database','session');

(OR)
Initializing session class in your controller. Once load session value set session information value $this->session.

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

Session will create unique id to encrypt(md5). It strong check session data.

Adding session data


The user information to get array or object format add that value in session separate name.

$user=array('username'=>'karthick');
$this->session->set_userdata('users',$user);

If you want update session you can set same session value. it will update same session array value.

Retrieving Session Data.


Use below syntax retrieve session value. store separate variable to get that information.
$user = $this->session->userdata('users');

Get all session value

$this->session->all_userdata()


Removing Session Data

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

Sometime added more data in session it will not take. especially adding eCommerce add to cart functionality it will not take more than session value. that time you have to create session table to store session information in table.

I have strongly suggested to solve this issues of session. Follow below link tutorial to easily solve that issue.

Link : http://www.2my4edge.com/2015/05/problem-in-adding-more-products-to-cart.html


Thank you for visiting. if have any queries write comments below.
"Sometimes we stare so long at a door that is closing that we see too late the one that is open. "
                                             
                                         - Alexander Graham Bell








Related Topics

No comments:

Post a Comment