All Articles

Creating a stateless request in Magento

Have you ever wanted to create a stateless request in Magento? Something that doesn’t touch any of Magento’s sessions?  We were having issues with some of the ajax calls on our cart and checkout pages mucking with the user’s cart and had get stateless on these calls.  The issue we were having was our checkout page was loading, then a javascript include was going out and bringing code from a 3rd party relevance engine into our dom, which was in turn calling back an ajax request to our servers.  This issue with this being that at the start of the page load, the checkout session was being set to a certain state.  This state was then being sent through the rest of the page load, and the ajax calls. Unfortunately, by the time the ajax call got back to our server, the session was different in both locations, creating a race condition.  The ajax request usually won, removing the work the full page load had done with trying to process checkout.  The good news was there was nothing in the ajax call that needed to touch the session, it was just some data lookup. So, nix the session part of that call, and our troubles should be over… Magento’s api controller is the only place that implements a stateless request this but its fairly easy to do (after a bit of digging).

As long as MageCoreControllerVarienAction is a parent in your controller’s hierchy, you are good to go (it probably is).  This class has a const FLAGNOSTART_SESSION which looks promising. Digging into the code a little we see that it controls whether cookies are processed or the session is started:

        if (!$this->getFlag('', self::FLAG_NO_START_SESSION)) {
            $checkCookie = in_array($this->getRequest()->getActionName(), $this->_cookieCheckActions);
            $checkCookie = $checkCookie &amp;&amp; !$this->getRequest()->getParam('nocookie', false);
            $cookies = Mage::getSingleton('core/cookie')->get();
            if ($checkCookie &amp;&amp; empty($cookies)) {
                $this->setFlag('', self::FLAG_NO_COOKIES_REDIRECT, true);
            Mage::getSingleton('core/session', array('name' => $this->_sessionNamespace))->start();

By adding to the preDispatch() method of our Action or Controller we can toggle this:

class Ai_AjaxCatalog_Controller_Action extends Mage_Core_Controller_Front_Action
	public function preDispatch()
		$this->setFlag('', self::FLAG_NO_START_SESSION, 1); // Do not start standard session
		return $this;

Now, any action in this controller will be stateless and not effect any sessions.

Published 17 Oct 2011

Code. Comics. Crossfit
Tim Broder on Twitter