How can I make a back link on PHP pages?

How can I link to the previous page that was visited using PHP?

+4
source share
7 answers

There are several ways to do this:

1) Use browser history - this option depends on whether JavaScript is enabled in the browser:

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

2) Use the Referer HTTP address , which usually contains a URL that references the current request:

 $link = $_SERVER['HTTP_REFERER']; 

3) Use some server-side mechanism that keeps track of the submitted pages and accordingly populates the "href" attribute in the "back" link. This can usually be useful in wizards; in other cases, the overhead may be too high. In addition, you must carefully handle the situation when a user opens a website on several browser tabs.

UPDATE

4) Create specific Back links based on the page relationship hierarchy. See Reply from Matti Virkkunen for details. In fact, this is the most valid way to navigate pages with "hierarchical" relationships ("view a list of items", "section> subsection", "view an object" and "nested objects", etc.).

+11
source

You very rarely want to have your backlink based on browser history, headings or referrer sessions. This is what the browser back button is on.

Backlinks are most useful when a user comes from outside your site, for example, from a search engine, and they would like to get a page that is β€œone back” in your page hierarchy. This may be the previous page, the top level in the tree, etc. Obviously, when a user comes from outside your website, kludges browser histories or header hooks will not work. The only way to create the right feedback is to understand the hierarchy of your page and create the right link for each view in the code.

+3
source

Here is a small example of how you can implement it:

 if(isset($_SERVER['HTTP_REFERER'])) { echo "<a href=".$_SERVER['HTTP_REFERER'].">Go back</a>"; } 
+1
source

The only way you can guess where the user came from is if you sent the Referrer header or not when they requested your PHP page. The contents of this header are stored in the variable $_SERVER['HTTP_REFERRER'] .

So you can write for example this

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

I do not know that this is related to SQL, although

0
source

You don't even need PHP.

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

Using history.back() or $_SERVER['HTTP_referer'] not the best way. The problem is that the user comes to your site from a direct link, google (or etc search engine), another site or similar traffic. Then this solution returns the user to the previous page, outside your site.

0
source

Use $ _SERVER ['HTTP_REFERER']

 $page_referrer=$_SERVER['HTTP_referer']; 

Use the link $ page_referrer as a link to the previous page! Warning: do not do this on the home page!

-1
source

Source: https://habr.com/ru/post/1337782/


All Articles