dhardy
dhardy

Reputation: 1003

Loop all Magento Cookies & Delete

I have a Magento site with a location to IP plugin. It uses cookies heavily. Because of this, I need to clear all cookies magento sets. I have what I believe is the correct code but it's not working:

$cookies = Mage::getModel('core/cookie')->get();
foreach($cookies as $cookie)
{
     Mage::getModel('core/cookie')->delete($cookie->name, $cookie->path);   
}

Some cookies are set on the path '/' and some on /another'. I would like to clear all to avoid any confusion.

Any ideas on how I can do this? Thanks!

Upvotes: 2

Views: 5611

Answers (2)

bylerj
bylerj

Reputation: 93

You are getting an error because $cookie->name and $cookie->path are not objects. To get your loop to work try this.

$names = Mage::getModel('core/cookie')->get(); //This returns an array of all cookies
foreach($names as $name) { //loop through the array
    $cookie = Mage::getModel('core/cookie')->get($name); //get the cookie object for each cookie
    $path = $cookie->getPath(); //get the path for the cookie

    Mage::getModel('core/cookie')->delete($name, $path); //delete that cookie
}

Upvotes: 2

Steve Robbins
Steve Robbins

Reputation: 13812

You need to clear the sessions too, e.g.

Mage::getSingleton('checkout/session')->unsetAll();

Take a look at Mage_Persistent_IndexController::unsetCookieAction() (store.com/persistent/index/unsetCookie/)

Upvotes: 0

Related Questions