Entry
Why does $HTTP_REFERER return an empty string?
Jun 13th, 2002 12:16
Philip Olson, greg leturk, Richard Lynch, Nathan Wallace,
We'll discuss a few possible reasons:
First, are you trying to use $HTTP_REFERER inside a function? If so,
you'll need to either add a 'global $HTTP_REFERER' line to your
function to get at the variable from the global scope or pass it in as
a argument. For example :
function foo()
{
global $HTTP_REFERER;
echo "Referrer is : $HTTP_REFERER";
}
http://php.net/manual/language.variables.scope.php
Second, in order for HTTP_REFERER to exist, there needs to be a
HTTP_REFERER header. This header does not exist if someone types in
the URL directly (ie: does not follow a link to the page). It only
exists when someone clicks on a link from another page. And, some
browsers don't send them.
Thirdly, your server may also be configured to not provide
$HTTP_REFERER in the first place. The PHP directive register_globals is
what creates it otherwise it's only available within the
$HTTP_SERVER_VARS and $_SERVER arrays. register_globals has recently
been defaulted to off (as of 4.2.0). So:
// Worked since PHP 3 (forever)
print $HTTP_SERVER_VARS['HTTP_REFERER'];
// Worked since PHP 4.1.0
print $_SERVER['HTTP_REFERER'];
Fourthly, be sure to check spelling. You may notice that referrer is
misspelled as referer in this variable, read a explanation on that
here :
http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?query=referer
Lastly, HTTP_REFERER is not passed by some older browsers.
This is a 'predefined variable', read up on it and other such variables
here:
http://www.php.net/manual/language.variables.predefined.php