How can I link to the previous page that was visited using PHP?
-
keep track of every page being visited in session variable – Shakti Singh Jan 31 '11 at 14:03
-
4@Shakti that's usual mistake most noobs do make. – Your Common Sense Jan 31 '11 at 14:09
7 Answers
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).

- 7,680
- 3
- 29
- 39
-
Sorry, but -1 for all the times I've ended up on a page that does something like that from a Google result. – Matti Virkkunen Jan 31 '11 at 17:40
-
-
One question: what is there a semicolon on the first example? I curious if I'm missing something about HTML. – Chris Happy Apr 24 '17 at 22:12
-
The first example is not pure HTML since it uses JavaScript to jump back in browser history. This example won't work with JavaScript disabled (when using NoScript for example). – Rico Mar 29 '18 at 07:35
You will very rarely want to have your back link based on browser history, referer headers or session. That is what the browser's back button is for.
Back links are the most useful when the user has come from outside your website, for instance, from a search engine, and they would like to get the page that's "one back" in your page hierarchy. This could be a previous page, an upper level in a tree, et cetera. Obviously when the user has come from outside your website, browser history kludges or referer header hacks aren't going to work. The only way to make a proper back link is to understand your page hierarchy and generate the correct link for each view in code.

- 63,558
- 9
- 127
- 159
Here is a small example on how you can implement it:
if(isset($_SERVER['HTTP_REFERER'])) {
echo "<a href=".$_SERVER['HTTP_REFERER'].">Go back</a>";
}

- 2,676
- 7
- 32
- 36
-
It will not write any link, since the information is not available (ex: you open the browser directly on this specific page). You may insert an "else" and inform the user of this, if needed. – Danilo Jan 31 '11 at 14:12
-
The best and most simple way to create a history based back links to add to hierarchy-based breadcrumbs esp on eCommerce sites – Hooman Askari Dec 22 '13 at 09:59
Using history.back()
or $_SERVER['HTTP_referer']
isn't best way.
Problem is when user come to your site from direct link, google (or etc search engine), another site or similar traffic. Then these solution return user to previous page, outside your site.

- 146
- 1
- 6
The only way you can guess where the user came from is whether they sent you a Referrer header or not when they requested your PHP page. The content of that header is stored in the $_SERVER['HTTP_REFERRER']
variable.
So, you could for example write it like this
<a href="<?=$_SERVER['HTTP_REFERRER']?>">Back to whence you came</a>
Don't really know what this has to do with SQL though

- 64
- 2
You don't even need PHP.
<a href="javascript://" onclick="history.back();">Back</a>

- 10,746
- 2
- 45
- 57
Use $_SERVER['HTTP_REFERER']
$page_referrer=$_SERVER['HTTP_REFERER'];
Use $page_referrer as link to the previous page! Warning: don't do this on homepage!