3

I'm building a Magento sandbox environment and need to set a session cookie based on a value in the query-string. The value in the query-string will be used to set an account ID for another service used within this Magento environment. This is not a production, public facing environment, so security is not a concern.

My first challenge is figuring out where to actually place the code within the Magento MVC. All users accessing this system will come directly through the root domain (as opposed to a specific page within the site... say a product or product category page). The bookmarked URL will look something like this:

http://www.somedomain.com/index.php?accountID=12345678

Below is the initial code I wrote to evaluate the query-string and set the cookie value:

$account_id = $this->getRequest()->getParam('accountID');
$account_id_cookie = Mage::getSingleton('core/cookie');
$account_id_cookie->set('account_id',$account_id,time()+86400,'/');

Where is the best place to put this code within the Magento MVC? I tried the root index.php file but it doesn't work. I suspect the best place is within a controller but I'm not sure which controller file is appropriate.

Once this value is set within a session cookie, it will be referenced in a JavaScript snippet located within the footer of the site so it's necessary for the value to be present in the cookie prior to the footer include being parsed.

1 Answer 1

4

You can observe the controller_action_layout_render_before event (you can read this if you don't know how do event/observer works)

Or you can rewrite the footer block Mage_Page_Block_Html_Footer (some documentation here)

In all case, you have to deactivate the cache or remove the cache directives in the footer block.

Your Answer

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

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