Magento session management (Core/Checkout/Customer)

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')->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?

2 thoughts on “Magento session management (Core/Checkout/Customer)

  1. In order to use my custom namespaced session, I had to register the MyModuleName_Session resource in the config.xml. Experienced Magento developers probably know this, but for beginners I don’t think it’s so trivial.

    In the module’s config.xml one should add:

    MyCompanyName_MyModuleName_Model
    MyModuleName_Session

    Correct me, if I’m wrong.. 🙂

  2. I precisely wished to thank you so much again. I’m not certain what I would have carried out without the type of pointers contributed by you directly on this industry. It truly was a real scary situation in my view, nevertheless considering a new well-written avenue you treated it took me to jump with contentment. I will be grateful for the support as well as expect you really know what an amazing job that you’re undertaking educating the mediocre ones with the aid of your blog post. I know that you’ve never come across all of us.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.