Magento 1.7 – How to Implement Back Button

magento-1.7url

I want to implement a "back button" which will takes the user to the previous page on clicking.
Then the page before that, then the page before that and so on.
So far I have done something like this..

<?php
      $url = Mage::app()->getRequest()->getServer('HTTP_REFERER');
      if ((strpos($url, Mage::app()->getStore()->getBaseUrl()) !== 0) && (strpos($url, Mage::app()->getStore()->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK, true)) !== 0)) {
      $url = Mage::app()->getStore()->getBaseUrl();
}
?>
<div class="link2Back"><a href="<?php echo $url; ?>">Back to ... </a></div>  

but it just going back to the one recent previous page only.
What are the other possible methods for this problem ?

Best Answer

have you tried using javascript?

<a href=”#” onClick=”Javascript:history.back();”>GO back</a>

The only problem with this, is that when a user enters your product page from let's say, goolge, the user will be redirected to google.

You can also take a look at this threat: https://stackoverflow.com/questions/5285031/back-to-previous-page-with-header-location-in-php