Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between the different Magento session types

Tags:

I looking for some information about the difference of the different session types available in Magento.

There's a core session, a customer session and a checkout session. But I'm not quite sure when to use which one and how they might behave differently. Are they all valid for the same time or does a checkout session get invalidated earlier than the core session?

like image 676
Flo Avatar asked Mar 21 '12 11:03

Flo


People also ask

How many types of sessions are there in Magento 2?

Magento2 provides five types of sessions: Magento\Backend\Model\Session– This session is used for Magento backend. Magento\Catalog\Model\Session– Catalog session is used for the frontend for product filters. Magento\Checkout\Model\Session– Checkout session is used to store checkout related information.

What is session in Magento?

A session is a way of storing variables and making them available on multiple pages on a website. In other words, a session is a temporary object that is created on the server for each Magento 2 store users to store some values, i.e., items in a cart.

What is core session in Magento 2?

So what is a session in Magento 2? A session is a way of sorting and storing variables on every page of Magento 2 stores. It allows your users to temporarily store some values on your store such as items in the shopping carts, personal information, etc.


1 Answers

Great question!

To answer the question directly: All session models lifetime are the same. Session lifetime is determined by configuration in Magento and in your server software. What you are probably intending to ask (in the Magento way of handling various sessions) is, "How long is the data for a given session type persisted?"

The answer is one of implementation, so the best way is to search the code for instantiation points. The search pattern to use is getSingleton('core/session') (or whichever session model). Anywhere that this is called - if it's the first time it's encountered - will create the session namespace (explained below) in the $_SESSION superglobal.

So, sessions are never "killed", but the data gets cleared depending on the implementation. The one that does this notoriously is checkout/session, as the data gets wiped after an order is placed.

Beyond this, you can rely that session is there for your persistence needs.

Session models in Magento use an abstract base class to define an API of sorts, Mage_Core_Model_Session_Abstract. This class fills the following roles/functions:

  1. Session namespacing via the init() method, literally separating stored values for each type under $_SESSION[$namespace]
  2. Getters for (connection to) the session-related configuration settings (including cookie lifetime, SID, security settings, etc.)
  3. Flash message storage and retrieval (addError(), addMessage(), addNotice(), and addSuccess())
  4. Getter for session storage configuration and methods
  5. Overloading (magic getters and setters) for setting params at will through Varien_Object::__call(). *Note that sessions have a modified magic getter which allows you to retrieve a datum from session and unset it with one call (e.g. $session->getSomeParam(true))

So, if you want your module to have its own session namespace, simply declare a session model as extending from the session abstract and in the protected _construct() call $this->init('namespace').

All data for session models will be set in array keys under the session namespace; for core this would be:

$session = Mage::getSingleton('core/session')->setSomeValue('Some string'); 

could be represented as

$_SESSION['core']['some_value'] = 'Some string' 
like image 154
benmarks Avatar answered Dec 09 '22 18:12

benmarks