2012-10-31 68 views
4

我希望我的网站能够找到计算机位置,所以如果有人从伦敦或曼彻斯特等访问我的网站,并根据他们的计算机位置显示某个区域的用户。有点像在线约会网站,建议您所在地区的用户。GEOIP和获取计算机的IP地址位置?

我一直在寻找这个GEOIP数据库,列出了世界各地的所有城市。但我不知道接下来要做什么?我是否需要查看获取IP地址脚本来比较GEOip数据库中的信息?

请有人指点我正确的方向。谢谢。从

geoip的数据库: http://dev.maxmind.com/geoip/geolite

回答

4

尝试下面的代码。

$ip = $_SERVER['REMOTE_ADDR']; 
echo $location = file_get_contents("http://api.hostip.info/country.php?ip=$ip"); 

不要在你的本地主机上试试这个。它会给ip 127.0.0.1。

echo $location= file_get_contents("http://api.hostip.info/country.php?ip=12.215.42.19"); 
//outputs US 
3

PHP已经内置函数为GeoIP的

GeoIP library

这一个功能可能是特别有用,因为它会在阵列中返回大陆,国家,城市等。

geoip_record_by_name

使用$_SERVER['REMOTE_ADDR']您的IP。

$record = geoip_record_by_name($_SERVER['REMOTE_ADDR']);

0

你可能已经安装GEOIP作为一个PECL扩展。如果你不这样做,那么你可以自己安装它。

如果你需要比国家更具体的任何东西,你将不得不支付得到数据库,但功能仍然存在。

1

请注意,这个数据库是:In our recent tests, the GeoIP databases tested at 99.8% accurate on a country level, 90% accurate on a state level in the US, and 83% accurate for cities in the US within a 40 kilometer radius.

我会考虑类似的:PEAR GEOLITE上库

使用这个库的快速片段在PHP中:

$geoip = Net_GeoIP::getInstance(dirname(__FILE__) . '/data/GeoLiteCity.dat'); 
$ipaddress = '72.30.2.43'; // Yahoo! 
$location = $geoip->lookupLocation($ipaddress); 
var_dump($location); 

输出将显示,类似的东西来:

object(Net_GeoIP_Location)[2] 
    protected 'aData' => 
     array 
     'countryCode' => string 'US' (length=2) 
     'countryCode3' => string 'USA' (length=3) 
     'countryName' => string 'United States' (length=13) 
     'region' => string 'CA' (length=2) 
     'city' => string 'Sunnyvale' (length=9) 
     'postalCode' => string '94089' (length=5) 
     'latitude' => float 37.4249 
     'longitude' => float -122.0074 
     'areaCode' => int 408 
     'dmaCode' => float 807 
4

这取决于您的需要。我建议你使用Web服务,这样你就不必担心托管数据库并自己维护数据库。您只需解析访问者的IP地址即可获得结果。我正在使用this site的IP2location Web服务。

+0

嗨HexaHow - 我在你的文章中改变了一些东西。您是否已经找到了格式化答案的帮助(例如添加链接)? – Lars

相关问题