Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do a "back" link on PHP pages?

Tags:

php

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

like image 796
Dan Avatar asked Jan 31 '11 14:01

Dan


1 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).

like image 180
Kel Avatar answered Sep 28 '22 16:09

Kel