首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IP地址的位置检测技术

IP地址的位置检测技术
EN

Stack Overflow用户
提问于 2010-04-04 21:41:24
回答 8查看 33K关注 0票数 20

IP地址的位置检测技术有哪些?

我知道要看

$_SERVER['HTTP_ACCEPT_LANGUAGE'] (不准确,但主要用于检测位置,例如,如果某个IP范围的用户将其浏览器设置为法语,则表示该范围属于法国)

gethostbyaddr($_SERVER['REMOTE_ADDR']) (查看国家代码顶级域)

然后可能是whois gethostbyaddr($_SERVER['REMOTE_ADDR'])

有时:

$HTTP_USER_AGENT (火狐的用户代理字符串有语言代码,不准确,但主要用于检测位置)

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

但是城市呢?

EN

回答 8

Stack Overflow用户

发布于 2012-07-08 02:55:12

如果你正在寻找一些复制-粘贴的解决方案,我找到了这个代码:

代码语言:javascript
复制
    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;

}

希望这对某些人有帮助。

票数 5
EN

Stack Overflow用户

发布于 2010-04-04 21:48:32

有一些数据库(有些是免费的)可以用来将IP转换成城市:

例如:http://www.maxmind.com/app/geolitecity

票数 3
EN

Stack Overflow用户

发布于 2018-02-08 12:35:14

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

下面是一个使用API的示例

代码语言:javascript
复制
curl https://ipapi.co/city/ 
> Mountain View

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

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

更多信息请点击这里:Location from IP address

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2574542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档