What's the difference between the different Magento session types
Asked Answered
A

1

24

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?

Ascension answered 21/3, 2012 at 11:44 Comment(0)
T
26

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'
Tillett answered 21/3, 2012 at 13:32 Comment(4)
Thx for the response, but that was not exactly what I was looking for. My question was more related to the different session types that already exist in Magento by default. Like the core, customer and checkout session. I wonder whether they have all the same lifetime or does one of them get invalidated earlier? E.g. checkout session sounds to me like it only exists until an checkout is completed. But I'm just guessing, that's why I want to know more about it.Ascension
Well, now you know the foundation :-)! The answer is, it's a matter of implementation. Grep the core for Singleton('checkout/session') and you'll get a list of everywhere checkout session could be initialized.Tillett
I adjusted my answer to actually answer your question!Tillett
Thanks man! I'm sure you helped me and a lot of other people!Ascension

© 2022 - 2024 — McMap. All rights reserved.