| Gzu521.com我的学习网 |
|
因为没有数据库的关系,我使用了一个免费的基于文本文件的统计程序(veryok 实用统计3.0 - 正式版 build 0530),发现统计出来的ip都是127.0.0.1,一时也不知道是什么原因,当然也注意到getenv(’remote_addr’)在有些环境下能得到正确的ip,有些环境下却是不行。自己水平有限,于是求助google,找到下面这个答案,顺便把其中的bug改了过来,这下正常了。 上面提到的统计程序的修改方法很简单 打开statadd.php文件,查找到$user_ip=getenv("remote_addr") 替换成$user_ip=getip(); 当然别忘了把下面的getip()函数添加到该文件中,保持好就可以了。 p.s. php manual中提及的使用getenv(’remote_addr’)来获取客户端ip的方法存在不少问题,所以有必要考虑采用更为完善的方法来比较精确的获取用户客户端的ip。 getenv (php 3, php 4, php 5) getenv -- gets the value of an environment variable description string getenv ( string varname ) returns the value of the environment variable varname, or false on an error. // or simply use a superglobal ($_server or $_env)$ip = $_server[’remote_addr’];?> 这是在php官方的manual提供的方法。 但是当web服务器api是asapi (IIS)的时候,getenv函数是不起作用的。这种情况下你如果用getenv来取得用户客户端ip的话,得到的将是错误的ip地址。 因此更为安全和准确的方法是尽量避免使用getenv函数。比如可以用以下的函数来获取ip信息: //get the real client ip ("bullet-proof") function getip(){ if (getenv("http_client_ip") && strcasecmp(getenv("http_client_ip"), "unknown")) $ip = getenv("http_client_ip"); else if (getenv("http_x_forwarded_for") && strcasecmp(getenv("http_x_forwarded_for"), "unknown")) $ip = getenv("http_x_forwarded_for"); else if (getenv("remote_addr") && strcasecmp(getenv("remote_addr"), "unknown")) $ip = getenv("remote_addr"); else if (isset($_server[’remote_addr’]) && $_server[’remote_addr’] && strcasecmp($_server[’remote_addr’], "unknown")) $ip = $_server[’remote_addr’]; else $ip = "unknown"; return($ip); } 更为详细的讨论请参见 http://cn.php.net/manual/zh/function.getenv.php
|
责任编辑:gzu521