30 October 2004
This post may be outdated due to it was written on 2004. The links may be broken. The code may be not working anymore. Leave comments if needed.

Generally

一般程序代码所书写的都是这样(如 ikonboard, UBB, UltraThreads etc.):
$IP_ADDRESS = $ENV{'HTTP_X_FORWARDED_FOR'} || $ENV{'REMOTE_ADDR'};
    这里有三种情况:
  • 通过透明代理服务器,那$ENV{'HTTP_X_FORWARDED_FOR'}为此代理服务器地址,而$ENV{'REMOTE_ADDR'}为你的地址。
  • 通过匿名代理服务器,同样$ENV{'HTTP_X_FORWARDED_FOR'}为此代理服务器地址,而$ENV{'REMOTE_ADDR'}为此代理服务器连接网页的地址(可与代理服务器地址不同)。
  • 没有使用代理服务器,那$ENV{'HTTP_X_FORWARDED_FOR'}为空,而$ENV{'REMOTE_ADDR'}为用户真实ip
举个例子会清晰点:
#proxy(anonymous) is 80.59.189.28,then got
$ENV{'REMOTE_ADDR'} = '80.58.3.235';
$ENV{'HTTP_X_FORWARDED_FOR'} = '80.59.189.28';
#no proxy
$ENV{'REMOTE_ADDR'} = '211.90.227.119';#my temp true ip
$ENV{'HTTP_X_FORWARDED_FOR'} = '';

Advance

其实另外还有两个ENV环境变量:'HTTP_CLIENT_IP', 'X_CLIENT_IP'。不过不太常用。如果增加此两变量,代码如下,前三者顺序可能有变(如 YaBB, X-Forum etc.):
$IP_ADDRESS = $ENV{'HTTP_X_FORWARDED_FOR'} || $ENV{'HTTP_CLIENT_IP'} || $ENV{'X_CLIENT_IP'} || $ENV{'REMOTE_ADDR'};

Especial

不适用于localhost/127.0.0.1
对于匿名/Anonymous Proxy来说,因为它不会将使用Proxy的人的ip一起发请求过来,所以获得使用Proxy人的ip是impossible mission.
获得Proxy相关信息的环境变量是$ENV{'HTTP_VIA'}.



blog comments powered by Disqus