Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unset session in Magento?

I am using session in my magento custom module.Below is my code

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

$session->setData("day_filter", 'weeks');
$session->setData("days", '5');          
$session->setData("next_delivery_date", '2012-05-12');

The above code is working fine, but now i want to unset or destroy all the value? Can you please provide me the solution how to unset all set values?

like image 322
user1443655 Avatar asked Jun 08 '12 04:06

user1443655


People also ask

How can I delete session in Magento 2?

To get folder to clear, run the command: php -r "var_dump(ini_get('session. save_path'));" And then delete this folder.

How many types of sessions Magento 2?

Magento2 provides five types of sessions: Magento\Backend\Model\Session– This session is used for Magento backend. Magento\Catalog\Model\Session– Catalog session is used for the frontend for product filters. Magento\Checkout\Model\Session– Checkout session is used to store checkout related information.

How can use custom session in Magento 2?

Steps to set Customer Session: You injection \Magento\Customer\Model\Session class to the constructor function of the Controller. Remove generated folder in magento source root by command: rm -rf generated/*


1 Answers

There are several ways to unset session variables in Magento. Most of these (not all) are defined in Varien_Object and so are avialable to all objects in Magento that extend it.

unsetData:

    $session->unsetData('day_filter');
    $session->unsetData('days');
    $session->unsetData('next_delivery_date');

uns (which will be marginally slower and ultimately executes unsetData anyway):

   $session->unsDayFilter();
   $session->unsDays();
   $session->unsNextDeliveryDate();

getData

Not a mistake! A relatively unkown method exists in Mage_Core_Model_Session_Abstract_Varien. The getData method in this class contains an optional boolean second parameter which if passed true will clear the variable while returning it.

So $session->getData('day_filter', true); would return the session variable day_filter and also clear it from the session at the same time.

Set to null:

   $session->setData('day_filter', NULL);
   $session->setData('days', NULL);
   $session->setData('next_delivery_date', NULL);

unsetAll | clear

Finally you could use the nuclear option (BEWARE: This will unset ALL DATA in the session, not just the data you have added):

$session->unsetAll(); or $session->clear(); (both aliases of each other)

like image 188
Drew Hunter Avatar answered Nov 15 '22 19:11

Drew Hunter