Magento 1 – When to Use Sessions and When to Use Cookies

cookiemagento-1session

I know Magento has following core functions for session and cookie handling:

Mage::getSingleton('core/cookie');
Mage::getSingleton('core/session');

When does Magento set Cookies and when PHP Sessions? What are the use cases?

I'm a bit confused, because I know Magento stores the cart information in Mage::getSingleton('checkout/session') and the status of the user in Mage::getSingleton('customer/session'). But the checkout session doesn't seem to be a session, but a cookie instead. Because when I close the browser and re-open the page the items are still in the cart.

Does that mean, that the checkout/sessionmodel sets cookies and not the global $_SESSION variable?

Best Answer

No, that means that sessions are persisted with a cookie, which contains the ID of the session. The only alternative in PHP is a URL parameter like ?SID=d41d8cd98f00b204e9800998ecf8427e where d41d8cd98f00b204e9800998ecf8427e is the session ID.

In Short, session data are stored on the server but since HTTP is stateless, a cookie or parameter with the session id is necessary to identify the right session.

Magento sets two session cookies, adminhtml for the backend and frontend for the frontend. Session models like 'checkout/session' store their data in a "namespace", i.e. $_SESSION['checkout'].

Session-unrelated cookies that Magento sets, are for example the store cookie that remembers, which store view you currently browse.

Related Topic