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.

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

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

Recommended Answers

All 6 Replies

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'];

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

'HTTP_REFERER'
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.

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

commented: good point +0

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

session_start();
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;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.