I learned a new technique (from Chris Coyier), and I thought it may be helpful for you guys.

Some of you guys may find this helpful if your creating a preview/demo page and you want to go back to original page. This could help you guys.

  $return = htmlspecialchars($_SERVER['HTTP_REFERER']);
  echo "<a href='$return'>return to page</a>"; 

Original source: http://css-tricks.com/snippets/javascript/go-back-button/

4 Years
Discussion Span
Last Post by broj1

thanks for pointing that out (i am still not a pro in php (as you can tell))... what do you recommend to do if using $_SERVER['HTTP_REFERER']?


I would think you'd want to test the value or make sure its not empty since you can visit a page by just opening a browser and typing in the URL. There would be no referrer in this case. I would assume that is what broj1 means when he says its not reliable.

I dont think there is an alternate recommendation, I think it just that there isnt always a referrer. Also you are depending on the browser to provide this info... what if the browser's developer decided not to use this "feature". I've learned that its always good to "test" before using to avoid exception errors and unwanted results.

This is pretty common across the server-side scripting languages..

in asp/asp.net we can access this info as well... Request.ServerVariables['HTTP_REFERER'];

Edited by JorgeM


This is what PHP manual says about the $_SERVER['HTTP_REFERER'] (see the link in my post):

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

Edited by broj1


Thank you, it can be helpful if the browser does not support javascript or it's disabled ...

Votes + Comments
good point

You could use session to store your previous page. Something like:

if(isset($_SESSION['current_page'])) {
    $_SESSION['prev_page'] = $_SESSION['current_page'];
$_SESSION['current_page'] = $_SERVER["SCRIPT_NAME"];

if(isset($_SESSION['prev_page'])) {  
    $link = '<a href="' . $_SESSION['prev_page'] . '">Go to previous page</a>';
} else {
    $link = '';
echo $link;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.