Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

last page 1

Status
Not open for further replies.

TheConeHead

Programmer
Aug 14, 2002
2,106
US
is there a way in PHP (not javascript) to retrieve the page someone came from like if you wanted to make a back button?

[conehead]
 
Yes!

Code:
$back = $_SERVER["HTTP_REFERER"];
echo '<a href=\"'.$back.'\">Go back: '.$back.'</a>';

It only works if that page was arrived on via a link, as opposed to typing in the URL.

[cheers]
Cheers!
Laura
 
In addition, to be safe, you could code it as follows:

Code:
if ($_SERVER["HTTP_REFERER"]){
  $back = $_SERVER["HTTP_REFERER"];
  echo '<a href=\"'.$back.'\">Go back: '.$back.'</a>';
}
else
  echo "<a href=\"javascript:history.go(-1);\">Go Back</a>";

That way, if the user types in the URL, you just won't have a broken link.

[cheers]
Cheers!
Laura
 
beware:

php manual said:
'HTTP_REFERER'

The address of the page (if any) which referred the user agent to the current page. [highlight]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. [/highlight]


-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top