0

People come to my website from variety of other websites and I would like to know these referral urls? How can I do it in PHP?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Vonder
  • 4,033
  • 15
  • 44
  • 61

3 Answers3

4

You can get the referrer using $_SERVER['HTTP_REFERER']. Note that it's very easy for the client to spoof this, so don't rely on it for anything important.

You can find out about other $_SERVER variables here.

Cam
  • 14,930
  • 16
  • 77
  • 128
  • Why is it easy to spoof this? Is there a reliable solution? – Vonder Dec 11 '10 at 20:13
  • 1
    @Vafello: It's easy to spoof because it's sent by the client. Anything sent by the client can be changed before it gets to your server. – Cam Dec 11 '10 at 20:16
  • But say someone put a link to my website on twitter. I would like to check if it was from twitter by using this method. Is it easy to fake then? – Vonder Dec 11 '10 at 20:19
  • 1
    @Vafello: Yes, but I wouldn't worry about it. If your reasons for checking the referrer are purely statistical, you don't really have to be concerned. Just be aware that your data isn't entirely reliable. – Cam Dec 11 '10 at 20:22
  • The point is that they are vaery important for me, not only from statistical perspective. So I understand it is easy to define the referral using a bit of php code, so it cannot be reliable. Maybe an answer to that is to count each IP only once... – Vonder Dec 11 '10 at 20:26
  • @Vafello: You could do that. It doesn't stop each person individually from spoofing the referrer, but most of your visitors are probably not malicious. On the other hand, be careful with IP's, since nowadays some ISP's will give large groups of their customers the same IP. – Cam Dec 11 '10 at 20:32
  • To add more quirks to the referrer... its also important to note that if the visitor arrived by some JavaScript code on the other site that directed them to you, the referrer may very well be blank if the visitor is using Internet Explorer (BUG): http://bit.ly/ctuuml – scunliffe Dec 11 '10 at 20:49
  • Thanks a lot Cam, of course I will accept your answer. One more thing though - there is a number os server variables, so I can get the site's IP, url and other stuff and check if it is facebook or twitter for example. I need only a few of them. How can a user fake them if it is in PHP code and he doesnt now which values to fake? So for exaple I can count it if the url and ip matches facebook and then chceck for user's IP and count it as a valid click??? – Vonder Dec 11 '10 at 20:50
  • @Vafello: Getting the referrer's IP is just as unreliable as getting the referrer's url. It's like there are two people sitting at a table - the client and the server. The client writes down the requested information on a piece of paper and passes it to the server - the client is expected to write certain things, but can actually write whatever he/she wants. So just be careful. – Cam Dec 11 '10 at 21:04
  • I know, what I meant was - I can get referrer site's name, ip, possibly other things (let's say it is twitter). Then check if they both match twitter's ip and url. How would a possible spoofer know which variabes I am using? Most likely he will only pass url. Maybe it's not perfect, but I guess it would work in 95% of cases?? – Vonder Dec 11 '10 at 21:23
  • @Vafello: First of all, I'm not even certain you can ask for the referer's IP independantly from its URL, and even if you can, it's barely marginally better. – Cam Dec 11 '10 at 21:59
0
$ref = getenv("HTTP_REFERER");

or

$ref = $_SERVER['HTTP_REFERER'];
code_burgar
  • 12,025
  • 4
  • 35
  • 53
0

Use the system variable:

$_SERVER['HTTP_REFERER']

Check this answer for more details.

Determining Referer in PHP

Community
  • 1
  • 1
Roadmaster
  • 5,297
  • 1
  • 23
  • 21