不同的浏览器,不同的IP?
我通过将 $_SERVER['REMOTE_ADDR']
的值保存在 MySQL 数据库中来保存用户的 IP 地址。问题是,对于 Firefox 和 Chrome,$_SERVER['REMOTE_ADDR']
都是 ::1
(这意味着 IPv6 中的 localhost),而对于 IE Opera 是 127.0.0.1
(IPv4)。
所以,我的问题是
IP 版本是否依赖于浏览器? (我曾经认为这取决于 计算机)
我应该在数据库中创建两个字段,一个用于 IPv4 地址,一个用于 IPv6 地址吗?
我应该将所有 IP 统一为 IPv6 吗?我怎样才能在 PHP 中做到这一点(如果可能的话)?
I'm saving the user's IP addresses by saving the value of $_SERVER['REMOTE_ADDR']
in a MySQL database. Problem is that for both Firefox and Chrome $_SERVER['REMOTE_ADDR']
is ::1
(that means localhost in IPv6) and for IE and Opera is 127.0.0.1
(IPv4).
So, my questions are
Are IP versions browser-dependant? (I used to think it depended on
the computer)Should I create two fields in the database, one for IPv4 addresses and one for IPv6 ones?
Should I unify all IPs to IPv6? And how can I do this in PHP (if it's even possible)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
相当明显 - 您的盒子启用了 IPv6,Firefox/Chrome 在可用时使用 IPv6,而 IE 和 Opera 则不使用(或者默认设置为关闭)。
将地址存储在足以容纳 IPv6 地址的字符串中。
不,因为在一般情况下你不能。
Fairly obvious - your box is IPv6-enabled, Firefox/Chrome use IPv6 whenever available, while IE and Opera don't (or it's a off-by-default setting).
Store the address in a string that's long enough to hold an IPv6 address.
No, 'cause in the general case you cannot.
使用 PHP 函数
inet_pton
将人类可读的 IP 地址转换为其打包表示形式。然后,您可以将每个 IP 地址存储在数据库的BINARY(16)
或VARBINARY(16)
字段中。Use the PHP function
inet_pton
to convert human readable IP addresses to their packed representation. You can then store each IP address in aBINARY(16)
orVARBINARY(16)
field in your database.浏览器将使用任何可用的东西。这可以是 IPv4 或 IPv6,甚至可以在会话期间更改。最重要的是,请记住,主机可以有许多 IPv6 地址,因此它也可能在会话期间发生变化。
简而言之:不要过多依赖 REMOTE_ADDR 的值:-)
The browser will use whatever is available. This can be IPv4 or IPv6, and that can even change during the session. On top of that keep in mind that a host can have many IPv6 addresses so it might change during the session as well.
In short: don't depend on the value of REMOTE_ADDR too much :-)