2012-10-31 66 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

相關問題