0

I am trying to set the cookie on user login basis and want to empty on logout. Login working perfect but logout didn't work. For that purpose I used this XML code :

    <customer_logout>
    <observers>
<customer_is_loggedout_observer>
                <type>model</type>
                <class>Easylife_Sales_Model_Observer</class>
                <method>customerLogout</method>
            </customer_is_loggedout_observer>
        </observers>
    </customer_logout>
        <controller_action_predispatch>
                <observers>
                    <customer_is_logged_in_observer>
                        <class>Easylife_Sales_Model_Observer</class>
                        <method>setHandle</method>
                    </customer_is_logged_in_observer>
                </observers>
            </controller_action_predispatch>
       <sales_quote_collect_totals_before>

And PHP code is in observer :

<?php

class Easylife_Sales_Model_Observer 
{

    public function setHandle(Varien_Event_Observer $observer)
    {
  Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('customer/session', array('name'=>'frontend'));
if($session->isLoggedIn()){

     $customerData = Mage::getSingleton('customer/session')->getCustomer();
   $name = 'loginuser';
          $value = $customerData->getId();

          Mage::getModel('core/cookie')->set($name, $value);

 }else if(Mage::getModel('core/cookie')->get('loginuser') > 0 ){

      Mage::getSingleton('customer/session')->loginById(Mage::getModel('core/cookie')->get('loginuser'));
 }


}

     public function customerLogout(Varien_Event_Observer $observer)
    {

      if (isset($_COOKIE['loginuser'])) {
         unset($_COOKIE['loginuser']);
        setcookie('loginuser', null, -1, '/');
      }
      Mage::getSingleton('checkout/session')->unsetAll();
       $cookieName = 'loginuser';
     $session = Mage::getSingleton("core/session", array("name"=>"frontend"));
     $session->unsetAll();
     $session->clear();

       //echo Mage::getModel('core/cookie')->get('loginuser');exit();

    }

}

I am trying to delete the cookie value and user session on logout click. It shows message of logout successfully and redirect to home page but when reload page it logged in? What I am doing wrong? The else condition is for multi-website where I want to logged in user when he logged in on one site.

  • use Mage::getModel('core/cookie') to delete cookie as well. might be issue with domain name and http only flag issue – Minesh Patel Jul 11 '17 at 4:47
0

You should use the type singleton for your observer and configure it under the frontent events:

Example for customer login

use

Namespace_Module_Model_Observer logout singleton

    <customer_login>
        <observers>
            <your_unqiue_event_name1>
                <class>Namespace_Module_Model_Observer</class>
                <method>login</method>
                <type>singleton</type>
            </your_unqiue_event_name1>
        </observers>
    </customer_login>
</events>

Then in your observer class set/unset your custom session value

in

public function login()
{
     ..... 
     Mage::getSingleton('customer/session')->setMyValue($myValue);
     .....
}

public function logout()
{
    .....
    Mage::getSingleton('customer/session')->unsMyValue();
    .....
}

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.