Working with Cookies in Magento: Create, Read and Remove
Magento 1 wraps PHP’s cookie functions in its own core/cookie model. Using it keeps the store’s cookie domain, path and lifetime settings consistent with everything else Magento sets.
The methods
Here are the calls for creating, reading and deleting a cookie, with every optional argument shown:
<?php
Mage::getModel('core/cookie')->set($name, $value, $period, $path, $domain, $secure, $httponly);
/**
* get cookie with a specific name
* $name = name of the cookie
*/
Mage::getModel('core/cookie')->get($name);
/**
* get all cookies as an array
*/
Mage::getModel('core/cookie')->get();
/**
* delete/remove cookie
* $name is mandatory; other parameters are optional and cen be set as null
*/
Mage::getModel('core/cookie')->delete($name, $path, $domain, $secure, $httponly);
?>
Tips
- Set the secure and HttpOnly flags for anything sensitive, so it is only sent over HTTPS and cannot be read by JavaScript.
- Keep values small and never store personal data in a cookie.
- Remember cookie consent rules if the cookie is not strictly necessary.
Heads up: this guide applies to Magento 1, which stopped receiving official security updates in June 2020. If your store still runs it, plan a move to Magento 2 or Adobe Commerce soon. We can help with the migration.
Need a second pair of eyes on this? Our team handles Magento development every week. See how we can help.