首页 > 语言 > 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教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选