[ACCEPTED]-How can I do a "back" link on PHP pages?-php

Accepted answer
Score: 13

There are several ways to do that:

1) Use 18 browser history - this option relies on 17 JavaScript being enabled in browser:

<a href='javascript:history.back(1);'>Back</a>

2) Use 16 Referer HTTP header, which typically contains URL, which referred 15 to current request:


3) Use some server-side 14 mechanism, which tracks passed pages, and 13 fills "href" attribute in "back" link 12 correspondingly. This generally can be useful 11 in wizards; in other cases overhead can 10 be too high. Also, you should carefully 9 handle situation, when user opens website 8 in several browser tabs.


4) Build specific 7 "Back" links based on page relations 6 hierarchy. See answer from Matti Virkkunen 5 for details. Actually, it's the most valid 4 way to navigate through pages with "hierarchical" relations 3 ("list view > element view", "section 2 > subsection", "object view 1 > nested object view" etc).

Score: 3

You will very rarely want to have your back 14 link based on browser history, referer headers 13 or session. That is what the browser's back 12 button is for.

Back links are the most useful 11 when the user has come from outside your website, for 10 instance, from a search engine, and they 9 would like to get the page that's "one back" in 8 your page hierarchy. This could be a previous 7 page, an upper level in a tree, et cetera. Obviously 6 when the user has come from outside your 5 website, browser history kludges or referer 4 header hacks aren't going to work. The only 3 way to make a proper back link is to understand 2 your page hierarchy and generate the correct 1 link for each view in code.

Score: 1

Here is a small example on how you can implement 1 it:

if(isset($_SERVER['HTTP_REFERER'])) {
    echo "<a href=".$_SERVER['HTTP_REFERER'].">Go back</a>";
Score: 0

The only way you can guess where the user 6 came from is whether they sent you a Referrer 5 header or not when they requested your PHP 4 page. The content of that header is stored 3 in the $_SERVER['HTTP_REFERRER']variable.

So, you could for example 2 write it like this

<a href="<?=$_SERVER['HTTP_REFERRER']?>">Back to whence you came</a>

Don't really know what 1 this has to do with SQL though

Score: 0

You don't even need PHP.

<a href="javascript://" onclick="history.back();">Back</a>


Score: 0

Using history.back() or $_SERVER['HTTP_referer'] isn't best way. Problem is when 4 user come to your site from direct link, google 3 (or etc search engine), another site or 2 similar traffic. Then these solution return 1 user to previous page, outside your site.

More Related questions