Magento 1 – Why Core Session is Empty?

core-sessionmodulesessionsocial-buttons

I have a custom module, which basically adds social media login in magento. However I am facing an issue with core/session which I dont know how to resolve.

I am adding my custom blocks in customer login page via my own layout xml file. Every block is loading successfully. One of my cusotm block is setting a variable in core/session through it's _construct() method as like this.

File : app\code\local\Mynamespace/Mymodule/Block/Entity/Someblock.php

<?php
class Mynamespace_Mymodule_Block_Entity_Someblock extends Mage_Core_Block_Template
{
     protected function _construct() {
        parent::_construct();

        // CSRF protection
        $csrf = md5(uniqid(rand(), true));
        Mage::getSingleton('core/session')->setGoogleCsrf($csrf);
        $this->client->setState($csrf);

    }

}

I ensured that, session variable is set when the page loads. Now in the next process/request, I need to validate google_csrf variable that is set in core/session. But the problem is Mage_Core_Model_Session appears to be empty in the next process. Suppose I need to validate session during the request of url www.domain.com/frontname/google/connect, then I checked core/session in the corresponding controller file like this.

File : app\code\local\Mynamespace/Mymodule/controllers/GoogleController.php

public function connectAction()
{
   print_r(Mage::getSingleton('core/session'));die();
}

But it outputs Mage_Core_Model_Session with no variable set in it. Why it happens like this?

More info: actually the block in question is used to show google button in frontend. When click on that button, it will force user to authenticate him with google account. I am checking core/session in the controller that processes the redirect url. I am getting the response successfully. But core/session that set before this action seems empty.

You have any thoughts about it ? Let me know.

Best Answer

I suspect the controller processing the redirect URL may be getting the core/session model before the customer's session has been re-started.

Mage_Core_Controller_Varien_Action::preDispatch starts the session back up by calling Mage_Core_Model_Session::start (inherited from Mage_Core_Model_Session_Abstract_Varien). This is responsible for starting or resuming a user's session, allowing the various session models to reconnect with their data.

Instantiating a session model before the controller's preDispacth has re-started the session will result in a session model that is empty. This may result from getting the instance in the controller's _construct method, even if it is only actually read from after preDispatch.

Related Topic