Get second last url in Magento 1.9
-
07-02-2021 - |
Question
How can we get the second last url in Magento 1.9?
I currently use this: <?php echo Mage::getSingleton('core/session')->getLastUrl();?>
To get the last URL.
But I want to get the second last URL after adding a product to the cart. Because the code above give in that case the url to add the product to the cart.
How can I get the url before that, so the second last?
Solution
You can use
Mage::app()->getResponse()->setRedirect($_SERVER['HTTP_REFERER']);
to get the second last URL or Referrer URL. Or if you need it as a string
Mage::app()->getResponse()->setRedirect($_SERVER['HTTP_REFERER'])->getHeaders()[2]['value'];
$_SERVER['HTTP_REFERER'] would output the url or you may use
$previousurl = Mage::helper('core/http')->getHttpReferer() ? Mage::helper('core/http')->getHttpReferer() : Mage::getUrl();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange