1

I'm working for a company that has chosen to use the db for session handling in magento. Specifically, in /app/etc/local.xml there is this entry:

    <session_save><![CDATA[db]]></session_save>

I understand that the data is being saved in the table core_session. However, I'm not familiar with how to read from and write to the session object.

with session_start() it's easy, I just write

$_SESSION['status']='OK'; //write
$status= $_SESSION['status']; //read

What is the equivalent method when magento is using db as a session storage method? I assume it's a class method. Thanks.

Oliver Williams
  • 5,966
  • 7
  • 36
  • 78

1 Answers1

5

Each module in Magento can have its own session object for saving namespaced values to/from the session. For example, to set the variable foo_bar in the "core" session namespace, all you need to do is call

Mage::getSingleton('core/session')->setFooBar('Some Value');

To do the same thing in the "customer" session namespace,

Mage::getSingleton('customer/session')->setFooBar('Some Value');

You'd then fetch these values with

Mage::getSingleton('core/session')->getFooBar();
Mage::getSingleton('customer/session')->getFooBar();

The basic idea is Magento provides you with these session objects so you don't need to worry about starting/stopping the session, or managing collisions in $_SESSION. Behind the scenes Magento's still using $_SESSION and session_start -- but its handling those details for you so you can use a session model/singleton just like you'd use any model in Magento.

You also might find this answer useful.

Community
  • 1
  • 1
Alana Storm
  • 164,128
  • 91
  • 395
  • 599