0

I want to prevent that Magento creates a session-file (eg sess_r8mgn8n6oep1roajrimu5ako83) under var/session when my nouserAction() gets called.

So far I tried to set the FLAG_NO_START_SESSION-flag, but it seems to create a session file anyway.

class Company_Mymodule_UserController extends Mage_Core_Controller_Front_Action{

    /*
     * @see app/code/core/Mage/Api/Controller/Action.php
     */
    public function preDispatch()
    {
        $this->getLayout()->setArea($this->_currentArea);
        $this->setFlag('', self::FLAG_NO_START_SESSION, 1); // Do not start standart session
        parent::preDispatch();
        return $this;
    }

    public function nouserAction(){
        echo "Don't create a user session at all if this action gets called!';
    }
}

Also in the backend under Customers -> Online Customers the "user" is displayed, even though it's not my intention.

  • $this->getLayout()->setArea($this->_currentArea); might be setting the session by calling other functions, as you're getting the layout, then setting the flag after. Have you tried putting the flag before this line? – Ricky Odin Matthews Jan 20 '17 at 15:43
  • Thanks for the tip. Does not seem to have any effect. – pbaldauf Jan 23 '17 at 9:40
3

After digging deeper into the Magento code I realized the pre_dispatch()-method supports additional flags.

Controller won't start a user session

public function preDispatch()
{
    $this->getLayout()->setArea($this->_currentArea);
    $this->setFlag('', self::FLAG_NO_START_SESSION, 1); // Do not start standart session
    $this->setFlag('', self::FLAG_NO_PRE_DISPATCH, 1);
    $this->setFlag('', self::FLAG_NO_POST_DISPATCH, 1);
    parent::preDispatch();
    return $this;
}

When we inspect the Magento events via var_dump(Mage::getConfig()->getNode('frontend/events')); we realize two important things.

  1. controller_action_predispatch
    • Fires Mage_Log_Model_Visitor->initByRequest()
  2. controller_action_postdispatch
    • Fires Mage_Log_Model_Visitor->saveByRequest()

As we follow the controllers' inheritance chain (Mage_Core_Controller_Varien_Action) we see that the two flags FLAG_NO_PRE_DISPATCH/FLAG_NO_POST_DISPATCH don't dispatch the previous mentioned events and thus no user session gets started in Mage_Log_Model_Visitor.

Another interesting approach could be to remove the events from the global config.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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