Search code examples
phpapacheseo

Determine I.P. Address of Referring Site


I am currently working on a marketing module that keeps track of sites that brings traffic to our site. Is there a way to get the domain or I.P. address of the referring site using PHP? I believe HTTP_REFERER does not always show up on the $_SERVER global.


Solution

  • The HTTP_REFERER header has to be sent by the client's browser. You can't rely on it being sent.

    Scenarios when it does not get sent include:

    • The user enters the address by hand
    • The user opens a link in one of the big E-Mail clients who go through great lengths to obscure the REFERER
    • The user's browser is configured to block the referrer header (rare)
    • The user is switching protocols (i.e. a link on a http site pointing to a https one, or vice versa)

    In those cases, there is nothing you can do.

    If you control the linking site however, you could add a referrer ID in the GET parameter to the link:

    http://example.com/?from=mysite
    

    you could then parse the from parameter in your script.

    Converting the referrer string to an IP usually not a good idea, seeing as many IP addresses host dozens or hundreds of sites. The distinction which site the user came from will be lost that way.