溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP怎么根據(jù)IP獲取地理位置

發(fā)布時(shí)間:2021-08-12 10:59:16 來源:億速云 閱讀:131 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“PHP怎么根據(jù)IP獲取地理位置”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

根據(jù)IP獲取訪客所在國家/城市/經(jīng)緯度
安裝GeoIP擴(kuò)展:

sudo apt-get install libgeoip-dev
pecl install geoip-1.1.0

注意:Beta版要指定版本號.如果是apt安裝的PHP,直接安裝php5-geoip這個(gè)包即可.
php.ini中加入:

extension=geoip.so
geoip.custom_directory="/usr/share/GeoIP"

免費(fèi)下載GeoLiteCity數(shù)據(jù)庫(解壓后18MB):
http://dev.maxmind.com/geoip/legacy/install/city/

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
sudo mkdir -v /usr/share/GeoIP
sudo mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat

測試:

php -a
<?php
print_r(geoip_record_by_name('106.37.165.80')); //回車后按Ctrl+D運(yùn)行
Array
(
 [continent_code] => AS
 [country_code] => CN
 [country_code3] => CHN
 [country_name] => China //國家
 [region] => 22
 [city] => Beijing //城市
 [postal_code] =>
 [latitude] => 39.928901672363 //緯度
 [longitude] => 116.38829803467 //經(jīng)度
 [dma_code] => 0
 [area_code] => 0
)

在命令行用geoiplookup查看IP信息:

traceroute www.oschina.net

可見IP地址

 61.145.122.155
sudo apt-get install geoip-bin geoip-database
geoiplookup 61.145.122.155 -f /usr/share/GeoIP/GeoIP.dat
GeoIP Country Edition: CN, China

geoip-database提供的GeoIP.dat只能精確到國家.

geoiplookup 61.145.122.155 -f /usr/share/GeoIP/GeoIPCity.dat
GeoIP City Edition, Rev 1: CN, 30, Guangdong, Guangzhou, N/A, 23.116699, 113.250000, 0, 0

從maxmind官網(wǎng)下的數(shù)據(jù)庫GeoLiteCity則信息更詳細(xì).

geoiplookup 61.145.122.155 則同時(shí)顯示上述兩個(gè)數(shù)據(jù)庫的信息.

根據(jù)IP確定經(jīng)緯度與計(jì)算距離

可以用

geoip_record_by_name($_SERVER['REMOTE_ADDR'])

根據(jù)用戶IP確定經(jīng)緯度.
注意:

geoip_record_by_name()

返回的西經(jīng)和南緯是負(fù)數(shù).

5000米轉(zhuǎn)成經(jīng)緯度:
緯度 Latitude:  1 deg = 110852 m
經(jīng)度 Longitude: 1 deg = 111320*cos(lat) m
同一經(jīng)線上,相差一緯度約為 110852 米
同一緯線上,相差一經(jīng)度約為 111320*cos(lat) 米 (lat為該緯線的緯度)

<?php
//以當(dāng)前用戶經(jīng)緯度為中心,查詢5000米內(nèi)的其他用戶
$y = 5000 / 110852; //緯度的范圍
$x = 5000 / (111320*cos($lat)); //經(jīng)度的范圍
$sql = '
 select * from user where 
 lat >= ($lat-$y) and lat <= ($lat+$y) and 
 lon >= ($lon-$x) and lon <= ($lon+$x);
';

數(shù)據(jù)庫用戶表中設(shè)兩個(gè)字段,分別存儲(chǔ)用戶的經(jīng)度lat和緯度lon.

($lat-$y) <= lat <= ($lat+$y)
($lon-$x) <= lon <= ($lon+$x)

這個(gè)范圍是一個(gè)粗略的范圍,下面計(jì)算距離后把超過5公里的用戶去掉即可.

根據(jù)上面查詢出來的用戶的經(jīng)緯度,
用半正矢公式(Haversine)根據(jù)經(jīng)緯度計(jì)算兩點(diǎn)間距離:

<?php
function distance($lat1, $lon1, $lat2, $lon2) {
 $R = 6371393; //地球平均半徑,單位米
 $dlat = deg2rad($lat2-$lat1);
 $dlon = deg2rad($lon2-$lon1);
 $a = pow(sin($dlat/2), 2) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * pow(sin($dlon/2), 2);
 $c = 2 * atan2(sqrt($a), sqrt(1-$a));
 $d = $R * $c;
 return round($d);
}
echo distance(0, 0, -1, 0); // 111202米

然后就可以用uasort或array_multisort由近到遠(yuǎn)列出用戶了,比如有名為win,osx,lin這3個(gè)用戶:

<?php
$arr = array(
 'win' => array(
  'dis' => 1024,
  'age' => 31
 ),
 'osx' => array(
  'dis' => 512,
  'age' => 15
 ),
 'lin' => array(
  'dis' => 512,
  'age' => 25
 )
);
foreach($arr as $k => $v) {
 $sort['dis'][$k] = $v['dis'];
 $sort['age'][$k] = $v['age'];
}
//先按距離升序排序,如果距離相同,則按年齡降序排序
array_multisort($sort['dis'], SORT_ASC, $sort['age'], SORT_DESC, $arr);
echo json_encode($arr);
//{"lin":{"dis":512,"age":25},"osx":{"dis":512,"age":15},"win":{"dis":1024,"age":31}}

“PHP怎么根據(jù)IP獲取地理位置”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI