Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is my header("Location: $_SERVER['HTTP_REFERER']"); PHP function not working?

It works when I input

header("Location: http://www.google.com");

but it doesn't work when I have

header("Location: $_SERVER['HTTP_REFERER']");

I want to redirect the page to whatever page it came from.

like image 423
Simon Suh Avatar asked Nov 03 '11 19:11

Simon Suh


2 Answers

Try it: :)

if (!empty($_SERVER['HTTP_REFERER']))
    header("Location: ".$_SERVER['HTTP_REFERER']);
else
   echo "No referrer.";

However, for determining which page user came from, I'd rather use session variable, which gets reset at every page:

session_start();
echo "Previous page:", $_SESSION['loc'];
$_SESSION['loc']=$_SERVER['PHP_SELF'];

ps: This only works for local pages, you cannot track other websites.

like image 172
Rok Kralj Avatar answered Sep 29 '22 12:09

Rok Kralj


You might try:

header("Location: {$_SERVER['HTTP_REFERER']}");

I've had problems with variable expressions which contain quotes in strings without braces.

You also need to look out for $_SERVER['HTTP_REFERER'] simply not being set. Some user agents don't set it, some privary tools mask it, and you need to handle people coming to your page without a referrer.

like image 35
artlung Avatar answered Sep 29 '22 11:09

artlung