IP地址的位置检测技术是什么?
我知道看
$_SERVER['HTTP_ACCEPT_LANGUAGE'](不准确的,但大多是有用的检测位置,例如,如果一个IP范围内的用户设定的法国自己的浏览器,则意味着在此范围)属于法国
和
gethostbyaddr($_SERVER['REMOTE_ADDR'])(看国家代码顶级域名),
然后gethostbyaddr($_SERVER['REMOTE_ADDR'])
有时可能是whois :(
$HTTP_USER_AGENTFirefox的用户代理字符串有语言代码,不准确但主要用于检测位置)
我也知道如何获得时区,但它在新浏览器中不起作用.此外还有css问题可以检测访问者的历史记录,它可以用来查看他/她访问过的google和维基百科页面(google.co.uk,google.com.tr)
但是城市呢?
Tim*_*sen 10
如果没有将IP地址映射到城市/国家/提供商的数据库,则无法执行此操作.您可以使用ip2location等商业产品.AFAIK虽然没有免费的替代方案,因为维护这样的IP数据库是相当多的工作.免费替代:GeoIP2
更新: 如果您投入足够的时间,有几件事可以让您创建这样的数据库:
如果你正在寻找一些复制 - 粘贴解决方案,我发现这个代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
希望这有助于某人.
| 归档时间: |
|
| 查看次数: |
31094 次 |
| 最近记录: |