Today we are going to cover magento session management. Magento has created session models in the following four areas or sections of the website.

  • Core – this can be accessed using Mage::getModel(“core/session”) object. In this you can store global session data which you can use through out the site
  • Customer – this can be accessed using Mage::getModel(“customer/session”) object. In this you can store customer related session data which you can use related to customer areas of your website like account, login, forgotten password pages etc.
  • Checkout – this can be accessed using Mage::getModel(“checkout/session”) object. In this you can store checkout related session data which you can use related to checkout areas of your website like basket, one page checkout pages etc.
  • Admin – this can be accessed using Mage::getModel(“admin/session”) object. In this you can store admin related session data which you can use anywhere in the admin areas of your website.

The above classes have been created by Magento so that you can create same session variables with same name for different areas of the site.

Let’s have a look at the examples below -:

Here we are storing the different data in the same session name variable -:

Mage::getModel('core/session')->setData('foo','global data');
Mage::getModel('customer/session')->setData('foo','customer data');
Mage::getModel('checkout/session')->setData('foo','checkout data');
Mage::getModel('admin/session')->setData('foo','admin data');
Mage::getModel('your_custom_module/session')->setData('foo','custom data');

Here we are accessing the different data from the same session variable -:

Mage::getModel('core/session')-&gt;getData('foo'); Mage::getModel('<code>customer</code>/session')-&gt;getData('foo');
Mage::getModel('checkout/session')-&gt;getData('foo'); Mage::getModel('admin/session')-&gt;getData('foo'); Mage::getModel('your_custom_module/session')-&gt;getData('foo');

This way you have very less likely to having conflicts with other modules which are using session variables. The best way is to create your own session class if your module is highly using session variables otherwise you can use session variable name as unique as possible like namespace_modulename_foo to avoid conflicts.

How can you create your own session class?

If you try all the above model classes they all are extended by Mage_Core_Model_Session_Abstract class and in the same manner you can create your own session model class by extending the abstract class.

The key is to call the init() function of abstract class to register unique namespacing of your session class. So if you look at this class Mage_Checkout_Model_Session, it calls the init() function ‘checkout’ as argument.

public function __construct()
{ $this-&gt;init('checkout');
}

The above code actually represents $_SESSION['checkout']and when you do the following

Mage::getModel("checkout/session")-&gt;setFoo('some value for checkout data')

It represents $_SESSION['checkout'][‘foo’] =’some value for checkout data’;

In the same manner you can create you own session class Scommerce_Custom_Model_Session

class Scommerce_Custom_Model_Session extends Mage_Core_Model_Session_Abstract
{ public function __construct() { $this-&gt;init('custom_unique_name'); }
}

And store and access the session variable as follows -:

$custom = Mage::getModel('custom_unique_name/session');
$custom-&gt;setFoo('Some Value');
$custom-&gt;getFoo('Some Value');

Hope this article helped you in some way. Please leave us your comment and let us know what do you think?

Similar Posts