How to Use Sessions in Joomla!

March 08, 2010 | by Brian Edgerton

Session storage is a very important aspect of web applications. In its simplest form, a PHP session allows data to be stored temporarily on the server and accessed throughout a user's time on the site. When that user leaves the site or is inactive for a certain amount of time, the data is destroyed. While anonymous sessions are common, sessions are usually associated with user logins. When a correct username/password combination is entered, a session is created around that user's access information and then read and checked every time that user loads a page. As a developer, you can access this session functionality to enhance your extensions.

One practical illustration is the ever-present shopping cart. When you are using an online store, you will usually choose to add a few items to your cart while you are browsing. You may add items at different times, update quantities, or remove products. Instead of using database tables to temporarily store and access this data, using session data is faster and easier.

Joomla's JSession class has already taken care of the nitty gritty aspects of session storage. It provides a very simple interface to store and retrieve data from the user's session.

## Grab session ##
$session = JFactory::getSession();
$session->set('mymessage', 'here is some message text');
....
$mymessage = $session->get('mymessage');
echo $mymessage;
 
## You can also store arrays and objects ##
$cart = array();
$cart['items'][] = array('item_number' => 12345, 'name' => 'Joomla! Web Security');
$cart['items'][] = array('item_number' => 98765, 'name' => 'Beginning Joomla! Web Site Development');
$cart['shippingInfo'] = array('address' => '123 Main Street', zip => '83957');
$session->set('cart', $cart);
....
$cart = $session->get('cart');
## Make changes or add items to cart ##
$cart['items'][] = array('item_number' => 10294, 'name' => 'Learning Joomla! 1.5 Extension Development');
## Store it back to session; Now it contains updated information appended to original ##
$session->set('cart', $cart);
 
## Erase cart session data
$session->clear('cart');
 

There are a few notes that should be made in addition to the above code. You can specify a default value in the get() method much like in the JRequest library. Also, when multiple extensions run on a site, there is a possibility that you can run into naming conflicts in your session variables. For this reason, JSession allows you to create your own namespace.

## Namespace will be called 'uniqueName'
$session = JFactory::getSession();
$session->set('cart', $cart, 'uniqueName');
....
## If session cart is empty or not set, an empty array will be returned ##
$cart = $session->get('cart', array(), 'uniqueName');
$session->clear('cart', 'uniqueName');
 

Sessions provide a very convenient way to make data persistent without the need to constantly pass it through URL's or hidden form fields. They are very useful in instances where frequently accessed information can be loaded once to save resources on database queries. Check out the rest of the information at the JSession docs page to find out what else you can do with Joomla! sessions.

About the Author

Brian EdgertonBrian Edgerton is a full time web developer born, raised, and currently residing in Burlington, North Carolina. A Computer Science major in college, he developed a curiosity for all things electronic at a young age. When he is not furiously coding away in front of his computer, Brian enjoys sports, movies, books, and falling asleep in his recliner. Brian is a Zend Certified PHP5 Engineer. He is also currently working on a Master's of Software Engineering degree through Auburn University. In addition to PHP, Brian is also proficient in Javascript and CSS/XHTML and likes to pretend he knows a few things about Linux server administration as well.

You can check out Brian's website at EdgeWebWorks.com or follow him on Twitter @brian_edgerton.

Read More

blog comments powered by Disqus