简体   繁体   English

为什么有时无法读取ip地址,这个功能

[英]why cant read ip address sometimes, this function

I am using this function to get ip address of the user in my site but it cant read ipaddress sometimes. 我正在使用此函数获取我的网站中的用户的IP地址,但有时无法读取ipaddress。 I dont know that a user can hide the ipaddress or not ? 我不知道用户可以隐藏ipaddress吗? If a user able to do so then how can i get the ipaddress or any other solution to identify the local computer of the user so i can prevent that computer to open my site. 如果用户能够这样做,那么我如何获得ipaddress或任何其他解决方案来识别用户的本地计算机,以便我可以阻止该计算机打开我的网站。

Any suggestion would be greatly appreciated. 任何建议将不胜感激。

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);
}

Thanks a lot. 非常感谢。

Normally just echo $_SERVER['REMOTE_ADDR']; 通常只是echo $_SERVER['REMOTE_ADDR']; should do the job. 应该做的工作。 Otherwise, explain us, what you mean with "it cant read ipaddress sometimes" 否则,请解释一下,你的意思是“它有时无法阅读ipaddress”

Ip addresses aren't a sure-fire way of banning users, since they can use Proxies, or can have a dynamic IP. IP地址不是禁止用户的可靠方式,因为它们可以使用代理,或者可以拥有动态IP。 For instance, for some users, banning the IP would just mean they'd have to restart their router and they can access your site again. 例如,对于某些用户而言,禁止IP只意味着他们必须重新启动路由器,他们才能再次访问您的网站。

Depending on your site, a better way to deny someone access is to use user authentication (user login). 根据您的站点,拒绝某人访问的更好方法是使用用户身份验证(用户登录)。

The short answer is that there is no 100% guaranteed way of getting a user's IP address - $_SERVER['REMOTE_ADDR'] is your best bet but even that's not 100% reliable - especially for users on networks. 简短的回答是没有100%保证获取用户IP地址的方式 - $_SERVER['REMOTE_ADDR']是您最好的选择,但即使这样也不是100%可靠 - 特别是对于网络用户。

Anything that's passed over HTTP from the client could be blocked/spoofed. 从客户端通过HTTP传递的任何内容都可能被阻止/欺骗。

It's easier to work your problem the other way around - it's much easier (and more secure) to whitelist access rather than blacklist it - if you can work that way around, I'd go for it. 通过相反的方式处理问题更容易 - 白名单访问更容易(也更安全)而不是将其列入黑名单 - 如果你可以这样工作,我会去做。 It only requires something like a simple (in an Apache .htaccess or vhost configuration): 它只需要像简单的东西(在Apache .htaccess或vhost配置中):

Deny from all
Allow from mydomain.com

the only IP address you can get from the environment is REMOTE_ADDR one. 您可以从环境中获得的唯一IP地址是REMOTE_ADDR
The other silly strings in your code is no more than HTTP headers, optional ones. 代码中的其他愚蠢字符串不过是HTTP标头,可选的。 Can be faked, omitted, have wrong format, be empty etc. 可伪造,省略,格式错误,为空等。

I leave a conclusion for you to make. 我给你留个结论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM