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?
To get folder to clear, run the command: php -r "var_dump(ini_get('session. save_path'));" And then delete this folder.
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.
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/*
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With