I'm basically trying to create a button that will take the users back to a search results page of a directory. Is there any way to populate href
with the url of the previous page?
4 Answers
There are a few add-ons that allow you to do this without having to rely on PHP or Javascript.
You would want to have <a href="{last_page_visited}">Back to Search Results</a>
or you could do <a href="{one_page_visited}">Back to Search Results</a>
the nice thing about Mo Variables is that it allows you to grab up to 5 pages of history from the session.
With URL Helper you would want to use the following <a href="{referrer}">Back to Search Results</a>
They both offer similar functionality with each having something that other does not so evaluate them both and see which one suits your needs the best.
If you enable PHP this will output the previous URL. But HTTP_REFERER can be modified by the users browser.
<?=$_SERVER['HTTP_REFERER'];?>
I extended a bit on Justin's submission, checking to ensure the reference was from within my own site. Eg. I didn't want to send people back to the Google search results page.
{exp:ce_str:ing allow_eecode="yes|yes" php}
<?php
$referrer = '{referrer}'; // using extension url_helper (http://devot-ee.com/add-ons/url-helper)
$base_url = '{global:base_url}'; // site base path
if (strpos($referrer, $base_url) === 0) { // check if they start the same
echo '<a href="{referrer}">Go back</a>'; // add back link to page within site
}
?>
{/exp:ce_str:ing}