问题
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?
回答1:
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:
- Session namespacing via the
init()
method, literally separating stored values for each type under$_SESSION[$namespace]
- Getters for (connection to) the session-related configuration settings (including cookie lifetime, SID, security settings, etc.)
- Flash message storage and retrieval (
addError()
,addMessage()
,addNotice()
, andaddSuccess()
) - Getter for session storage configuration and methods
- 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'
来源:https://stackoverflow.com/questions/9803979/whats-the-difference-between-the-different-magento-session-types