Can't get the HTTP_REF.. to work

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
conthox
Forum Commoner
Posts: 39
Joined: Tue Jun 25, 2002 1:44 pm
Location: Sweden

Can't get the HTTP_REF.. to work

Post by conthox »

Hello

I want to check the URL that people came from, but I can't make it work! :(

What is the code I have to write?

Thanks for helping me!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

First of all you should read this from the manual:
'HTTP_REFERER'
The address of the page (if any) which referred the user agent to the current page. 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.
Aside from this issue you should use:

Code: Select all

$_SERVER['HTTP_REFERER']
to access this infomation (if available) for PHP version 4.1 and up or if you are using PHP version 4.0.6 or below:

Code: Select all

$HTTP_SERVER_VARS['HTTP_REFERER']
Mac
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post by Elmseeker »

Try:

Code: Select all

$ref = $HTTP_SERVER_VARS["HTTP_REFERER"];
echo "Thank you, you were sent to this page by ".$ref."!";
[Edit]

Heh, Sorry Twig...we must have been editing at same time lol.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

For PHP 4.1 or above (if you don't have the need to be compatible with older versions of PHP) it's better to use:

Code: Select all

$ref = $_SERVER['HTTP_REFERER'];
instead of

Code: Select all

$ref = $HTTP_SERVER_VARS['HTTP_REFERER'];
It cuts the typing down :) .

Mac
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post by Elmseeker »

True, anything that works the same way and saves keystrokes is a GOOD thing! I just upgraded to a newer than 4.1 version of PHP and REALLY need to get used to the shorter names of the super globals hehe...
conthox
Forum Commoner
Posts: 39
Joined: Tue Jun 25, 2002 1:44 pm
Location: Sweden

Post by conthox »

Thank you!

I really appreciate your help.
Post Reply