customer session is empty in custom module magento

2019-08-04 16:57发布

问题:

i have made a custom module which uses customer session, but its strange, on live site its not returning customer data. i have tried following methods:

$sessCustomer = Mage::getSingleton('customer/session', array('name' => 'frontend'));
    echo '<pre>';print_r($sessCustomer->getCustomer()->getData()); echo '</pre>';exit;

it returns:

Array
(
    [website_id] => 1
)

if i print the customer session:

Mage::getSingleton('customer/session')->getData();

this returns (exact output):

     array(
[_session_validator_data] => Array
        (
            [remote_addr] => 58.65.183.10
            [http_via] => 
            [http_x_forwarded_for] => 58.65.183.10
            [http_user_agent] => Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:16.0) Gecko/20100101 Firefox/16.0
        )

    [session_hosts] => Array
            (
                [bestevalue.info] => 1
            )
    [messages] => Mage_Core_Model_Message_Collection Object
            (
                [_messages:protected] => Array
                    (
                    )

                [_lastAddedMessage:protected] => 
            )
    [id] => 
)

i am exhausted here , i am logged in, can see the customer dashboard with customer info on it but not been able to use that session in my custom module, please guide me how to fix this.

Update:

i have checked in /app/etc/local.xml that session type is file

<session_save><![CDATA[files]]></session_save>

so is there different method of extracting session info with php ? what am i doing wrong?

Update 2:

i have used router as well to make pretty url public function match(Zend_Controller_Request_Http $request)

on start of this action i placed

Mage::getSingleton('core/session', array('name' => 'frontend'));

but still not working with router without one it is working for example directly accessing the action :

site.com/module/controller/action

it works but not with router. any thoughts? thanks,

回答1:

The solution is simple:

private function _getCustomerSession() {
    Mage::getSingleton('core/session', array('name' => 'frontend'));
    return Mage::getSingleton('customer/session');
}

This initializes the session and returns it. Call this function like this:

$this->_getCustomerSession()->getCustomer()

And it will have the data available.

Update for googlers and future reference: I had this issue again today and my own solution didn't work (like the question). Further investigation revealed I have overridden the preDispatch() function in my custom module to check if the module was enabled in backend and if not redirect to homepage with error. In this case the solution was to -obviously- call parent::preDispatch();



回答2:

The answer to this is that most likely the controller you are extending for your custom module action is not the correct class. I've also just had the same problem and after an hour or so of bashing my head on the screen I realized my controller was extended this

Mage_Core_Controller_Varien_Action

and not this

Mage_Core_Controller_Front_Action

I'm not sure without going into these what the main difference is but I know you wont have a customer session extending the first class!



回答3:

I suffered by not getting the customer session inside my custom module. It was working in my localhost fine but in server i couldn't able to get the customer session.

I have found the solution by replacing the frontname in config.xml,

i have just changed <frontName>votingsytem</frontName> to <frontName>vote</frontName>

i got the customer session it work fine in live server.

I hope this will be useful.

Before:

<frontend>
  <routers>
     <votingsytem>
        <use>standard</use>
        <args>
           <module>Vgs_Votingsytem</module>
           <frontName>votingsytem</frontName>
        </args>
     </votingsytem>        
  </routers>
  <layout>
     <updates>
        <votingsytem>
           <file>votingsytem.xml</file>
        </votingsytem>
     </updates>
  </layout>

After:

<frontend>
  <routers>
     <votingsytem>
        <use>standard</use>
        <args>
           <module>Vgs_Votingsytem</module>
           <frontName>vote</frontName>
        </args>
     </votingsytem>        
  </routers>
  <layout>
     <updates>
        <votingsytem>
           <file>votingsytem.xml</file>
        </votingsytem>
     </updates>
  </layout>



回答4:

So, I've spent more than 3 hours googling the reason, and at the end I randomly decided to remove the Controller constructor overriding and Yes!, the session came back!.

Yet, I was overriding it correctly by calling the parent constructor!.