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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

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

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post by Christopher »

What kind of server? I think that is mainly a Apache variable.
(#10850)
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

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

Post 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.
Post Reply