IP地址的位置检测技术

发布于 2024-08-27 14:41:36 字数 612 浏览 7 评论 0 原文

IP地址的位置检测技术有哪些?
我知道要看
$_SERVER['HTTP_ACCEPT_LANGUAGE'](不准确,但对于检测位置最有用,例如,如果某个 IP 范围的用户将其浏览器设置为法语,则意味着该范围)属于法国

gethostbyaddr($_SERVER['REMOTE_ADDR'])(查看国家/地区代码顶级域名)
那么可能是 whois gethostbyaddr($_SERVER['REMOTE_ADDR'])
有时:
$HTTP_USER_AGENT (Firefox 的用户代理字符串有语言代码,不准确,但主要可用于检测位置)

我也知道如何获取时区,但它在新版本中不起作用浏览器。 此外,还有 CSS 问题检测访问者的历史记录,它可以用来查看他/她访问过哪些谷歌和维基百科页面(google.co.uk、google.com.tr)

但是城市呢?

What are the location detecting techniques for IP addresses?
I know to look at the
$_SERVER['HTTP_ACCEPT_LANGUAGE'] (not accurate but mostly useful to detect location, for example if an IP range's users set French to their browser then it means that this range) belongs to France
and
gethostbyaddr($_SERVER['REMOTE_ADDR']) (to look country code top-level domain)
then may be to whois gethostbyaddr($_SERVER['REMOTE_ADDR'])
sometimes:
$HTTP_USER_AGENT (Firefox's user agent string has language code, not accurate but mostly can be used to detect the location)

Also I know how to get the time zone but it does not work in the new browsers.
Moreover there is css issue that detects visitor's history, it can be used to see what google and wikipedia pages he/she has visited (google.co.uk, google.com.tr)

But what about cities?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

雨后彩虹 2024-09-03 14:41:36

如果没有将 IP 地址映射到城市/国家/提供商的数据库,您就无法做到这一点。您可以使用一些商业产品,例如 ip2location据我所知,没有免费的替代方案,因为维护这样的 IP 数据库需要大量工作。 免费替代方案:GeoIP2

更新:
如果您投入足够的时间,有几件事可以让您创建这样的数据库:

  1. 使用 区域和地区提供的数据库本地注册中心查找 IP 所有者。
  2. 许多 ISP 使用允许您定位用户的命名模式。有时你可以
    如果您进行反向 DNS 查找,甚至可以读取纯文本形式的城市名称。有时是
    更加神秘。例如我目前有 p5dcf6c4a.dip.t-dialin.net ,并且我没有
    命名方案的想法是..
  3. 使用跟踪路由。如果您无法识别用户的位置,您仍然可以找到
    其上行链路的位置

You can't do this without a database that maps IP addresses to cities/countries/providers. There are commercial offerings such as ip2location that you could use. AFAIK there is no free alternative though, as maintaining such a IP database is quite a lot of work. Free alternative: GeoIP2

Update:
There are several things that allow you to create such a db, if you invest enough time:

  1. Use the databases provided by regional and local registries to find an IP's owner.
  2. Many ISPs use a naming schema that allows you to locate the user. Sometimes you can
    even read the city name in plain text if you do a reverse-DNS lookup. Sometimes it is
    more cryptic. For example I currently have p5dcf6c4a.dip.t-dialin.net , and I have no
    idea that the naming scheme is..
  3. Use a traceroute. If you can't identify the location of a user, you can still find out
    the location of its uplink
蓝咒 2024-09-03 14:41:36

如果您正在寻找一些复制粘贴解决方案,我发现了这段代码:

    function countryCityFromIP($ipAddr)
{
//function to find country and city from IP address
//Developed by Roshan Bhattarai http://roshanbh.com.np

//verify the IP address for the
ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
$ipDetail=array(); //initialize a blank array

//get the XML result from hostip.info
$xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);

//get the city name inside the node <gml:name> and </gml:name>
preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);

//assing the city name to the array
$ipDetail['city']=$match[2]; 

//get the country name inside the node <countryName> and </countryName>
preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);

//assign the country name to the $ipDetail array
$ipDetail['country']=$matches[1];

//get the country name inside the node <countryName> and </countryName>
preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
$ipDetail['country_code']=$cc_match[1]; //assing the country code to array

//return the array containing city, country and country code
return $ipDetail;

}

希望这对某人有帮助。

If you looking for some copy - paste solution, i foudn this code :

    function countryCityFromIP($ipAddr)
{
//function to find country and city from IP address
//Developed by Roshan Bhattarai http://roshanbh.com.np

//verify the IP address for the
ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
$ipDetail=array(); //initialize a blank array

//get the XML result from hostip.info
$xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);

//get the city name inside the node <gml:name> and </gml:name>
preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);

//assing the city name to the array
$ipDetail['city']=$match[2]; 

//get the country name inside the node <countryName> and </countryName>
preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);

//assign the country name to the $ipDetail array
$ipDetail['country']=$matches[1];

//get the country name inside the node <countryName> and </countryName>
preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
$ipDetail['country_code']=$cc_match[1]; //assing the country code to array

//return the array containing city, country and country code
return $ipDetail;

}

Hope this helps somebody.

╰ゝ天使的微笑 2024-09-03 14:41:36

您可以使用数据库或 API(托管数据库)将 IP 地址映射到其位置(城市、国家、邮政编码)、时区等。

使用 API 的示例:来自 IP 地址的位置

curl https://ipapi.co/city/ 
> Mountain View

curl https://ipapi.co/country/
> US

curl https://ipapi.co/timezone/
> America/Los_Angeles 

以下是更多

You can map an IP address to its location (city, country, postal code), timezone etc. with either a database or an API (hosted database).

Here's an example of using an API

curl https://ipapi.co/city/ 
> Mountain View

curl https://ipapi.co/country/
> US

curl https://ipapi.co/timezone/
> America/Los_Angeles 

more here : Location from IP address

沫尐诺 2024-09-03 14:41:36

你可以使用这个函数

 function ip_info(){
            return $data = json_decode(file_get_contents('http://ip-api.com/json/'),true);
        }

调用该函数

$info = ip_info();
var_dump($info);

U can use this function

 function ip_info(){
            return $data = json_decode(file_get_contents('http://ip-api.com/json/'),true);
        }

Call the function

$info = ip_info();
var_dump($info);
魂ガ小子 2024-09-03 14:41:36

我建议您使用自托管 IP 记录,而不是使用第三方 API。这篇文章向您解释了更多

http://www.techsirius.com/ 2014/05/simple-geo-tracking-system.html

基本上他们使用的是maxmind的乡村城市IP数据库记录[http://dev.maxmind.com/geoip/geoip2/geolite2/] 进行少量修改。之后是示例查询

$ip =  $_SERVER['REMOTE_ADDR'];
$long = sprintf('%u', ip2long($ip));
$sql = "SELECT * FROM `geo_ips` WHERE '$long' BETWEEN `ip_start` AND `ip_end`";

Instead of using third party API I would suggest you to use self hosted IP record. This post explain you more

http://www.techsirius.com/2014/05/simple-geo-tracking-system.html

Basically they are using maxmind's country city IP database record [http://dev.maxmind.com/geoip/geoip2/geolite2/] with few modifications. After that a sample query

$ip =  $_SERVER['REMOTE_ADDR'];
$long = sprintf('%u', ip2long($ip));
$sql = "SELECT * FROM `geo_ips` WHERE '$long' BETWEEN `ip_start` AND `ip_end`";
埖埖迣鎅 2024-09-03 14:41:36

编译这些数据库的一种方法是根据 whois 信息。例如,要确定 69.59.196.211 的位置:

fmark@home:~$ 主机 69.59.196.211
211.196.59.69.in-addr.arpa 域名指针 stackoverflow.com。
fmark@home:~$ whois stackoverflow.com

Whois 服务器版本 2.0

.com 和 .net 中的域名
现在可以注册域名
许多不同的竞争注册商。

<前><代码> ;

<强>>注册者:stackoverflow.com llc
410 克莱顿大道埃尔塞里托,
加利福尼亚州 94530 美国

<前><代码> ;

显然这并不是非常准确,因此据称使用了更复杂的技术。

One way in which these databases are compiled is from whois information. For example, to determine the location of 69.59.196.211:

fmark@home:~$ host 69.59.196.211
211.196.59.69.in-addr.arpa domain name pointer stackoverflow.com.
fmark@home:~$ whois stackoverflow.com

Whois Server Version 2.0

Domain names in the .com and .net
domains can now be registered with
many different competing registrars.

 <SNIP>

> Registrant: stackoverflow.com llc
410 Clayton Ave El Cerrito,
California 94530 United States

 <SNIP>

Obviously this isn't hugely accurate, so more sophisticated techniques are allegedly used.

似梦非梦 2024-09-03 14:41:36

如果地理定位功能对您的项目不是一个很大的要求,您可以使用像这样简单的东西:

$geotxt='';
$dt=json_decode(@file_get_contents('https://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']));
if ($dt!=null) $geotxt=$dt->country_name.', '.$dt->region_name.', '.$dt->city;
echo $geotxt;

您必须考虑到 freegeoip 的限制为 10k 请求/小时。

If the geolocation functionality is not a big requirement for your project, you can use something as simple as this:

$geotxt='';
$dt=json_decode(@file_get_contents('https://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']));
if ($dt!=null) $geotxt=$dt->country_name.', '.$dt->region_name.', '.$dt->city;
echo $geotxt;

You have to take into account that freegeoip has a limit of 10k request/hour.

三月梨花 2024-09-03 14:41:36

大多数现有数据库使用来自国际注册机构 ARIN、AFRINIC、ANIC 等以及用户提交的位置的数据聚合。更复杂的技术包括拓扑和约束地理定位。

我运行自己的 API ipdata.co 提供多个数据点,包括国家、城市、地区、纬度/经度等。

此答案使用“测试”API 密钥,该密钥非常有限,仅用于测试一些调用。 注册为您自己的免费 API 密钥,每天最多可收到 1500 个开发请求。

php > $data = json_decode(file_get_contents('https://api.ipdata.co/69.59.196.211?api-key=test'),true);
php > echo $data['country_name'];
//United States
php > echo $data['city'];
//Albany

Most of the existing databases use an aggregation of data from the international registries ARIN, AFRINIC, ANIC etc as well as user submitted locations. More sophisticated techniques include Topology and Constraints Geolocation.

I run my own API ipdata.co that provides several data points including, the country, city, region, lat/long and others.

This answer uses a 'test' API Key that is very limited and only meant for testing a few calls. Signup for your own Free API Key and get up to 1500 requests daily for development.

php > $data = json_decode(file_get_contents('https://api.ipdata.co/69.59.196.211?api-key=test'),true);
php > echo $data['country_name'];
//United States
php > echo $data['city'];
//Albany
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文