What's the difference between the different Magento session types

回眸只為那壹抹淺笑 提交于 2019-11-27 11:47:32

问题


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:

  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'


来源:https://stackoverflow.com/questions/9803979/whats-the-difference-between-the-different-magento-session-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!