1

I want to set data session when customer is not log in and if this customer logged in i want to retrive this data and i NEED to do it in observer. I used already for example this events to get data:

customer_login
controller_response_redirect
controller_action_predispatch

And for all of them my session data was cleared after logged in.

I set data like this:

$url = Mage::getSingleton('customer/session')->setBeforeAuthUrl(Mage::helper('core/url')->getCurrentUrl(), array('_secure' => true));
$test =  Mage::getSingleton('customer/session')->setData('test', 'cos tam');
$test123 =  Mage::getSingleton('core/session')->setData('test123', 'cos tam12345');

And try to get like this:

$url = Mage::getSingleton('customer/session')->getBeforeAuthUrl();
$test = Mage::getSingleton('customer/session')->getData('test');
$test123 = Mage::getSingleton('core/session')->getData('test123');

How should i store this session or/and what event should i use?

1 Answer 1

2

The session is customer based, not user based and since the guest and logged in user are different as far as Magento knows it's not possible.

The easiest way would be to use a cookie. This is stored on the users device and will be available both when the user is logged on and off.

$cookie = Mage::getSingleton('core/cookie');
$cookie->set('test', 'cos tam' ,time()+3600,'/');
$cookie->set('test123', 'cos tam12345' ,time()+3600,'/');

3600 is the number of seconds the cookie will be stored from the time it's set. In this case it's an hour but you can always increase it.

If, however, you're storing sensitive data don't put it in a cookie. Create a database table (read this article) with the fields

  • ID
  • key
  • IP
  • value

generate a random key for new visitors, store that in a cookie on their device and insert it in the table together with the value and users IP address. Now you can retrieve the data by checking for the cookie and it's value and using the IP to ensure there's no session hijacking.

The script would look something like this

$cookie = Mage::getSingleton('core/cookie');
if ($key = $cookie->get('secret_key')) { // user has cookie, retrieve data
   $collection = Mage::getModel('[namespace]_[module]/values')->getCollection()
      ->setFieldToFilter('key', $key)
      ->setFieldToFilter('ip', Mage::helper('core/http')->getRemoteAddr(true));

   $value = $collection->getFirstItem()->getData('value');

} else { // new user so set data
   $value = 'The Value You Want To Store';
   $key = uniqid();

   // save key for reference
   $cookie->set('secret_key', $key);

   // store in database
   Mage::getModel('[namespace]_[module]/values')->setData(array(
      'key' => $key,
      'ip' => Mage::helper('core/http')->getRemoteAddr(true),
      'value' => $value,
   ))->save();
}
0

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.