Determine I.P. Address of Referring Site

2019-04-12 15:12发布

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.

Thanks in advanced.

标签: php apache seo
5条回答
一纸荒年 Trace。
2楼-- · 2019-04-12 15:27

You can determine the reffering URL with $_SERVER['HTTP_REFERER'] but bear in mind this can be manipulated.

You can then use gethostbyname($referrer) to get the IP address.

See: http://php.net/manual/en/reserved.variables.server.php and http://php.net/manual/en/function.gethostbyname.php

查看更多
对你真心纯属浪费
3楼-- · 2019-04-12 15:38

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.

查看更多
我想做一个坏孩纸
4楼-- · 2019-04-12 15:43

If it does not show up in $_SERVER it usually means the client is not sending it. The referrer is data that cannot be trusted for accuracy as it is entirely up to the user (more specifically their browser).

Your best bet is to check if the referer is there, then use the gethostbyname() function on the domain to get the IP you want.

http://php.net/manual/en/function.gethostbyname.php

查看更多
Lonely孤独者°
5楼-- · 2019-04-12 15:46

The referring address is present in the request header from all cooperating browsers for which there is a referrer. If the user just typed in your URL, then there is no referrer.

It is a trivial programming matter to convert the URL into an IP address:

<?php
$raddr = gethostbyaddr($_SERVER['HTTP_REFERER']);

?>
查看更多
男人必须洒脱
6楼-- · 2019-04-12 15:46

If HTTP_REFERER does not show up in server, then that user did not enter your site by a link, or he has a way to mask that variable via his browser. Not much else you can do.

查看更多
登录 后发表回答