php 基于geoip的根据ip分配玩家所属的区域

    /**
     *  根据ip分配玩家所属的区域
     *  @author: lambertli
      *  modify_time: 16-12-28
      *
      *  用输出的数组来更新G_FUNC::getCountryArea
     */

    error_reporting(E_ALL);
    define('EARTH_RADIUS', 6378.137);//地球半径
    define('PI', 3.1415926);         //圆周率

    //ip配置文件地址
    $path = '/product/moba/alpha/server/htdocs/data/Data/ip.txt';
    //服务器地址
    $serevr = array();
    //txha_room1--德克萨斯  13.66.55.223     北纬30度 西经经度:-97度
    //fraha_room1-法兰克福  35.156.232.143   北纬52.2度 东经14.32度
    //hkha_room1--香港      103.215.191.176  北纬22度 东经113度
    $server['1'] = array('latitude'=>'30',    'longitude'=>'-97');
    $server['2'] = array('latitude'=>'52.2','longitude'=>'14.32');
    $server['3'] = array('latitude'=>'22',    'longitude'=>'113');


    /**
    * 计算两组经纬度坐标 之间的距离
    * params :lat1 纬度1; lng1 经度1; lat2 纬度2; lng2 经度2; len_type (1:m or 2:km);
    * return m or km
    */
    function getDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
    {
        $radLat1 = $lat1 * PI / 180.0;
        $radLat2 = $lat2 * PI / 180.0;
        $a       = $radLat1 - $radLat2;
        $b       = ($lng1 * PI / 180.0) - ($lng2 * PI / 180.0);
        $s       = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
        $s       = $s * EARTH_RADIUS;
        $s       = round($s * 1000);
        if ($len_type > 1)
        {
            $s /= 1000;
        }
        return round($s, $decimal);
    }

    /**
    * 获取ip对应的信息
    * params :ip
    * return ip对应的信息
    */
    function getCityInfoByIp($ip) {
        include ("geoipregionvars.php");
        if (function_exists("geoip_record_by_name")) {
            //返回数组
            if (false !== stripos($ip, '192.168') || false !== stripos($ip, '127.0')) {
                $geoip    = "202.157.180.184";
            } else {
                $geoip    = $ip;
            }
            @$record = geoip_record_by_name($geoip);
        } else {
            return null; // 如果没安装扩展直接返回
        }
        $arr = array();
        if($record){
            $record = (object)$record;
            $arr = array (
                'country' => $record->country_name,
                'province' => (isset($GEOIP_REGION_NAME [$record->country_code]) && isset($GEOIP_REGION_NAME [$record->country_code][$record->region])) ? $GEOIP_REGION_NAME [$record->country_code] [$record->region]:'',
                'city' => $record->city,
                'latitude' => $record->latitude,
                'longitude' => $record->longitude
            );
        }
        return $arr;
    }

    $jsonStr = file_get_contents($path);
    $data = json_decode($jsonStr, TRUE); 

    foreach ($data as $key => $value) {
        $dataList[$key] = getCityInfoByIp($value['IP']);
        $dis = 1000000000;
        $sever_id = 1;
        foreach ($server as $skey => $svalue) {
            $lag = getDistance($dataList[$key]['latitude'],$dataList[$key]['longitude'],$svalue['latitude'],$svalue['longitude'],2,0);
            $dataList[$key]['dis'.$skey] = $lag;
            if($lag<$dis){
                $dis = $lag;
                $sever_id = $skey;
            }
        }
        $dataList[$key]['dis'] = $dis;
        $dataList[$key]['sever_id'] = $sever_id;
    }
    foreach ($dataList as $key => $value) {
        echo "'".$key."' => ".$value['sever_id'].",";
    }

你可能感兴趣的:(php 基于geoip的根据ip分配玩家所属的区域)