How can I link to the previous page that was visited using PHP?
There are several ways to do that:
1) Use browser history - this option relies on JavaScript being enabled in browser:
<a href='javascript:history.back(1);'>Back</a>
2) Use Referer HTTP header, which typically contains URL, which referred to current request:
$link = $_SERVER['HTTP_REFERER'];
3) Use some server-side mechanism, which tracks passed pages, and fills "href" attribute in "back" link correspondingly. This generally can be useful in wizards; in other cases overhead can be too high. Also, you should carefully handle situation, when user opens website in several browser tabs.
UPDATE
4) Build specific "Back" links based on page relations hierarchy. See answer from Matti Virkkunen for details. Actually, it's the most valid way to navigate through pages with "hierarchical" relations ("list view > element view", "section > subsection", "object view > nested object view" etc).
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