Page 1 of 1

$_SERVER['HTTP_REFERER'] not working on the server ??

Posted: Thu Apr 24, 2008 11:20 pm
by PHPycho
Hello forums!!
Case:
In localhost:

Code: Select all

$_SERVER['HTTP_REFERER']
works fine but it shows error in the server as: " Undefined index: HTTP_REFERER". I dont know why its not working there.

Whats the solution for this: any setting should be made to php.ini or else ?

Thanks in advance for the help.

Re: $_SERVER['HTTP_REFERER'] not working on the server ??

Posted: Fri Apr 25, 2008 12:39 am
by Christopher
What kind of server? I think that is mainly a Apache variable.

Re: $_SERVER['HTTP_REFERER'] not working on the server ??

Posted: Fri Apr 25, 2008 1:59 am
by mchaggis
The variable $_SERVER['HTTP_REFERER'] is only created if the browser sends a Referer string in it's headers. So the site that you have set as your homepage or the first page you go to does not receive this as a header string as there is no actual referer. It is also worth remembering that it is possible to configure your machine to never send this infomation, commonly happens what using software to cover your tracks.

If the code is not affected and you just want to stop the error from showing in you log files, then you can do one of two things:

1) Check that the variable exists before you use it. (You would need to do this for every reference of the $_SERVER['HTTP_REFERER'] var:

Code: Select all

 
<?php
if (array_key_exists('HTTP_SERVER', $_SERVER)) {
    // Do what you want
}
?>
 
2) As you would have to do the above for every reference, you may want to just do the following at the begining of your script:

Code: Select all

 
<?php
if (!array_key_exists('HTTP_SERVER', $_SERVER)) {
    $_SERVER['HTTP_SERVER'] = '';
}
?>
 
Implementing one of the above will prevent you log files filling up.