国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP安裝GeoIP擴展根據IP獲取地理位置及計算距離的方法

2024-05-04 23:47:33
字體:
來源:轉載
供稿:網友

根據IP獲取訪客所在國家/城市/經緯度
安裝GeoIP擴展:

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

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

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

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

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gzgunzip GeoLiteCity.dat.gzsudo mkdir -v /usr/share/GeoIPsudo mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat

測試:

php -a
<?phpprint_r(geoip_record_by_name('106.37.165.80')); //回車后按Ctrl+D運行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 //經度 [dma_code] => 0 [area_code] => 0)

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

traceroute www.oschina.net 

可見IP地址

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

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

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

從maxmind官網下的數據庫GeoLiteCity則信息更詳細.

geoiplookup 61.145.122.155 則同時顯示上述兩個數據庫的信息.

根據IP確定經緯度與計算距離

可以用

geoip_record_by_name($_SERVER['REMOTE_ADDR'])

根據用戶IP確定經緯度.
注意:

geoip_record_by_name()

返回的西經和南緯是負數.

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

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

數據庫用戶表中設兩個字段,分別存儲用戶的經度lat和緯度lon.

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

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

根據上面查詢出來的用戶的經緯度,
用半正矢公式(Haversine)根據經緯度計算兩點間距離:

<?phpfunction 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由近到遠列出用戶了,比如有名為win,osx,lin這3個用戶:

<?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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 榆社县| 中牟县| 启东市| 旬邑县| 钟祥市| 中山市| 秦皇岛市| 新闻| 仙居县| 白朗县| 屏边| 利津县| 成安县| 峨山| 浦县| 陆丰市| 太和县| 孙吴县| 米泉市| 通城县| 长沙县| 阿坝县| 崇文区| 武穴市| 横山县| 陈巴尔虎旗| 阿荣旗| 东海县| 新绛县| 黔东| 永嘉县| 龙游县| 措美县| 华阴市| 团风县| 绥德县| 库尔勒市| 永安市| 齐齐哈尔市| 赤城县| 临朐县|