Special Offer - Get $90 Discount and Host Your Dream Web Site @ Dreamhost for 1 Year Click Here. Promo Code SLJ345
Back To Top
WordPress Web Application Development
Develop Powerfull Web Applications Using Cutting Edge WordPress Development Techniques

Codeigniter Session Variables in Controllers and Custom Classes

on 2011/04/24 9:38 AM stored in: Uncategorized and tagged:

Set and Get Codeigniter Session Variables in Controllers

Codeigniter provides a built in session library to manage all the session related activities in your application. Since most parts of your applicationion requires session handling, its better to load the session library automatically by configuring in the autoload.php file. Otherwise you will have to load the session library inside the constructor of every controller which uses sessions.

Go to the autoload.php file inside the config folder and change the $autoload['libraries'] array to the following code. Also you can load other libraries inside this
array.

 $autoload['libraries'] = array('session');

Now lets move on to setting and getting codeigniter session variables inside your controllers.

1. Set Session Variables in Controllers

               $shopping_cart = array(
			'user_id' => 1,
			'number_of_items' => '4',
                        'cart_value' => '34.55'
		);
		$this->session->set_userdata($shopping_cart);

2. Get Session Variables in Controllers

		$user_id      = $this->session->userdata('user_id');
		$items        = $this->session->userdata('number_of_items');
		$total_amount = $this->session->userdata('cart_value');
               

You can set session values using a single array or you can add them one by one using set_userdata method. Same method can be used inside your models and views.

Set and Get Session Variables in Custom Classes


1. Set Session Variables in Custom Classes


	       $ci = & get_instance();
               $shopping_cart = array(
			'user_id' => 1,
			'number_of_items' => '4',
                        'cart_value' => '34.55'
		);
		$ci->session->set_userdata($shopping_cart);

2. Get Session Variables in Custom Classes

		$ci = & get_instance();
		$user_id      = $ci->session->userdata('user_id');
		$items        = $ci->session->userdata('number_of_items');
		$total_amount = $ci->session->userdata('cart_value');
               

Libraries cannot be accessed in custom classes using $this keyword. $this is only allowed inside controllers,models and views. You have to use get_instance() method and pass it by reference to get the codeigniter super object. Once you assign the object to a variable, you can call the libraries in the same method using the variable.

Codeigniter Session Best Practices

In this section i am going to explain how to use codeigniter sessions variables in a better and manageable way. This method is not mentioned as best practice or recommended in the codeigniter site. I learned this through my experience in working with sessions.

 

Its better to divide your application into sections and store session variables according to the section. Consider a situation where user buy products in an e-commerce application with shopping cart. Here we can divide the process simply into login, selecting products and adding to cart and payments section. Once you use this method you know exactly what variables are available in the session at any given stage, which decreases the complexity and the confusion in managing session variables. Once the process is completed you can remove the session variables for that section without affecting other variables.

Before Dividing Into Sections

		$session_var = array(
			'user_id' => 1,
			'number_of_items' => '4',
                        'cart_value' => '34.55',
			'transaction_id' => '434324532',
			'payment_status' => 'completed'
		);
		$this->session->set_userdata($session_var);

This code is hard to manage since its not clearly defined which variables are for which stage. So we are not sure when to clear the variables in the session. Also we have to clear the variables one by one. Keeping data in session takes resources and it will decrease the performance of your site. So its essential to clear unwanted data in the session.

After Dividing Into Sections

		$session_var = array("login"=> array('user_id' => 1),
					"shopping_cart"=> array('number_of_items' => 4 ,'cart_value' => '34.55'),
					"payments"=> array('transaction_id' => '434324532','payment_status' => 'completed')
				);			
		);
		$this->session->set_userdata($session_var);

		// Get cart related details
		$cart_details = $this->session->userdata('shopping_cart');
		$cart_value = $cart_details['cart_value'];

		// Remove the session data by section
		$this->session->unset_userdata('shopping_cart');
		$this->session->unset_userdata('payments');

In the above method session data are divided into sections. So at any given moment we can clear the data of a section after it is completed. Also we can access the data related to particular process separately.

I think this is a better way of using session with codeigniter. Feel free to discuss advantages/disadvantages or any issues in using above method.

Leave a Reply

Follow Us On Facebook