php 中 $_SERVER['REMOTE_ADDR'] 的替代方案
是否有 $_SERVER['REMOTE_ADDR'] 的替代方案。它返回访问站点的计算机的 IP 地址。 我正在尝试搜索路由器分配的相同外部IP,并进入此: 两台计算机如何通过NAT? 并发现如果计算机连接到同一调制解调器,则会分配相同的外部IP。 我正在 php 中创建一个简单的登录程序,它使用 $_SERVER['REMOTE_ADDR'] 来确定用户是否已登录同一网络中的其他位置。如果这些计算机通过路由器连接到同一个调制解调器,这实际上将不起作用。
Is there any alternative to the $_SERVER['REMOTE_ADDR']. Which returns the ip address of the computer accessing a site.
I'm trying to search about same external ip assigned by router, and got into this:
How do two computers connect to same external address through NAT?
And found out that the same external ip is assigned if the computers are connected to the same modem.
I'm creating a simple login program in php which uses $_SERVER['REMOTE_ADDR'] to determine if a user is already logged in somewhere else in the same network. And this won't actually work if those computers are connected to the same modem through the router.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这是你能做的最好的了。服务器只知道请求来自哪里,并且可能是代理或NAT路由器或其他一些不是直接最终用户的实体。你对此无能为力,这就是网络的运作方式。
解决方案很简单:不要使用 IP 来识别用户。曾经。使用cookie。
No, this is the best you can do. The server only knows where the request is coming from, and that may be a proxy or a NAT router or some other entity which is not the direct enduser. There's nothing you can do about that, that's how networks work.
The solution is simple: Don't use IPs to identify users. Ever. Use cookies.
使用更传统的基于 cookie 的登录可以解决这个问题。浏览器通过提供唯一的令牌来识别用户。正如您所发现的,IP 并不是唯一的。
Using a more traditional cookie based login solves this. The browser identifies the user by providing a unique token. Ip, as you have discovered, is not unique.