php $_SERVER['REMOTE_ADDR'] 没有给出正确的 IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10982277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$_SERVER['REMOTE_ADDR'] not giving the right ip address
提问by zeldarulez
I'm making a form with PHP and I want to keep record of the User's IP Addresses. This is the snip-it of code I used:
我正在用 PHP 制作一个表格,我想记录用户的 IP 地址。这是我使用的代码片段:
<input type="hidden" name="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" />
When I open the code up in XAMPP and read the source, the value had an IP address different than what was mine:
当我在 XAMPP 中打开代码并阅读源代码时,该值的 IP 地址与我的不同:
<input type="hidden" name="ip" value="::1" />
Does this IP address normally happen when I use it in a localhost (XAMPP)?
If not, are there any alternatives into grabbing the user's IP address?
当我在本地主机 (XAMPP) 中使用它时,这个 IP 地址通常会发生吗?
如果没有,是否有其他方法可以获取用户的 IP 地址?
回答by Aleks G
IP ::1is "localhost" in IPv6 version. Your machine is configured with IPv6 - and hence you're getting this IP address. Probably, when you deploy your application on the live server, IPv6 will not be configured on the server and your app will get a more familiar IPv4 address (e.g. aaa.bbb.ccc.ddd).
IP::1在 IPv6 版本中是“本地主机”。您的机器配置了 IPv6 - 因此您将获得此 IP 地址。可能,当您在实时服务器上部署应用程序时,服务器上不会配置 IPv6,您的应用程序将获得更熟悉的 IPv4 地址(例如 aaa.bbb.ccc.ddd)。
On another note, $_SERVER['REMOTE_ADDR']may not always contain the right address. It's better to use:
另一方面,$_SERVER['REMOTE_ADDR']可能并不总是包含正确的地址。最好使用:
if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip_address = $_SERVER['REMOTE_ADDR'];
}
回答by Quentin
<input type="hidden" name="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" />
<input type="hidden" name="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" />
Don't do that. Get the request from $_SERVERwhen the form is submitted. Getting it when the form is generated and storing it in the form just gives people the opportunity to change it.
不要那样做。从$_SERVER提交表单时获取请求。在生成表单时获取它并将其存储在表单中只是为人们提供了更改它的机会。
Does this IP address normally happen when I use it in a localhost (XAMPP)?
Does this IP address normally happen when I use it in a localhost (XAMPP)?
Yes. Getting the local IP (IPv6) address is normal when you request a page from localhost.
是的。当您从 localhost 请求页面时,获取本地 IP (IPv6) 地址是正常的。
回答by Prash
'::1' is the IPV6 version of localhost (or 127.0.0.1).
'::1' 是 localhost(或 127.0.0.1)的 IPV6 版本。
Open port 80 and visit the page from your IP Address. Should work fine then :).
打开端口 80 并从您的 IP 地址访问该页面。应该可以正常工作:)。
回答by Nayjest
1). You don't need to add <?php echo $_SERVER['REMOTE_ADDR']; ?>to form. In this case it's easy to forge it (actually it's easy any case). Better add IP to data on server side.
1)。您不需要添加<?php echo $_SERVER['REMOTE_ADDR']; ?>到表单中。在这种情况下很容易伪造它(实际上在任何情况下都很容易)。最好将 IP 添加到服务器端的数据中。
2) You can look also to $_SERVER['HTTP_X_FORWARDED_FOR'].
If user have a proxy, some of them (transparent proxies) place real user's IP there.
2)你也可以看看$_SERVER['HTTP_X_FORWARDED_FOR']。如果用户有代理,其中一些(透明代理)将真实用户的 IP 放在那里。
3) Just note: Data about IP's isn't trustworthy at all.
3) 请注意:有关 IP 的数据根本不值得信赖。
回答by Kh?i
This is actually your IP. Albeit your IPv6 IP and not IPv4.
这实际上是您的IP。尽管是您的 IPv6 IP 而不是 IPv4。
In IPv6, ::1 stands for localhost / 127.0.0.1.
在 IPv6 中,::1 代表 localhost / 127.0.0.1。
回答by Max
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARTDED_FOR'] != '') {
$ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip_address = $_SERVER['REMOTE_ADDR'];
}
This code returns ip of the client. If you think this is the server IP, you are probably right because your server is (presumably) hosted on your pc. Since your client (the pc) and the server run on the same pc, they both have the same ip. If you don't understand this, you should really do some research into ips, local ips and all that stuff.
此代码返回客户端的 ip。如果您认为这是服务器 IP,那么您可能是对的,因为您的服务器(大概)托管在您的 PC 上。由于您的客户端(PC)和服务器在同一台 PC 上运行,因此它们都具有相同的 ip。如果你不明白这一点,你真的应该对 ips、本地 ips 和所有这些东西进行一些研究。

