从用户IP地址获取位置 [英] Get location form a users IP address

查看:127
本文介绍了从用户IP地址获取位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从使用的IP地址获取位置。有很多关于如何做到这一点的主题。我真的喜欢这个,它的工作原理非常清晰。
示例中使用的IP地址在德国的某处找到位置,但是当我使用我的IP地址(192.168.0.7)时,所有内容都会以未知方式返回。当网站只在线时,我不会使用我的IP地址,我将使用PHP找到用户的IP地址。

I am trying to get a location from a uses IP address. There is a lot of topics on how to do this. I really like this one, it works and is very clear. The IP address used in the example finds the location somewhere in germany, but when I use my IP address (192.168.0.7) everything comes back as unknown. When the site is only online I wouldn't be using my IP address I would be finding the users IP address using PHP.

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];

        }
$ip = '192.168.0.7'; //replace with my IP address

我也遇到了此网站找到我的IP地址,除了数字不同外,这是非常准确的。 2.126.219.101

I have also came across this site which finds my IP address, which is quite accurate except the numbers are different. 2.126.219.101

有没有办法可以测试我在localhost上使用上述方法获取我的IP地址,或者我的IP地址有问题。

Is there a way I can test what I have on localhost to obtain my IP address using the above method or is there something wrong with my IP address.

推荐答案

192.168.0.7 是你的私人 IP地址。它仅在您的本地(家庭,工作......)网络中有效。因为它仅在您的本地网络内有效,所以它不能用于位置。事实上,现在世界上可能有数百万台其他计算机在自己的网络中也拥有这个私有IP地址。

192.168.0.7 is your private IP address. It is only valid within your local (home,work...) network. Because it is only valid inside your local network, it cannot be used for location. In fact, there are probably millions of other computers in the world right now that also have this private IP address within their own networks.

2.126。 219.101 是您的公共 IP地址。至于本地网络以外的任何机器,这都是您的IP地址。它可用于指示位置。

2.126.219.101 is your public IP address. As far any machine outside of your local network is concerned, that is your IP address. It can be used to give an indication of location.

这篇关于从用户IP地址获取位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆