I have an observer attached to 'front_init_before' action:


It checks request string for a parameter and saves it in a session. Code from an Observer.php file:


Everything works up to now. Data is saved in the session at this point but when I want to read it from a .phtml file:

$custom_id = Mage::getSingleton('core/session')->getCustomId();

$custom_id is empty. Then, in my Observer.php, directly after setCustomId() I call:


and the 'get' function in phtml file returns the desired value.

Why get function from .phtml file works only when it's called in the Observer too?

1 Answer 1


When the controller_front_init_before event is dispatched, the session is not initialized yet. Your call to Mage::getSingleton('core/session') will initialize it, but the session model relies on values from the front controller, like the host name of the requets. Since the front controller is not initialized yet, it might be that you end up with a different session id (tbh, I'd have to look into the implementation details to see what exactly goes wrong)

A possible solution is to use Mage::registry() to save the value globally for the current request, and if you need to persist it in the session, do that at a later point, for example when you retrieve the value or in a different observer.

  • Thanks fschmengler! This was the source of the problem. One of the solutions is to use controller_action_predispatch event(as suggested by Marius in other post).
    – Alan
    Feb 23, 2015 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.