为什么地理位置给出错误的状态

时间:2016-10-04 09:24:33

标签: php geolocation

此代码用于地理位置

我在不同的区域尝试过此代码,但它给了我错误的州和城镇

我该如何解决这个问题?

我在黎巴嫩在贝鲁特尝试这项工作,但在贝鲁特以外的地方没有工作,它仍然给我贝鲁特州和贝鲁特镇

$ip=$ipaddress; 
       //Array ( [domain] => dslb-094-219-040-096.pools.arcor-ip.net [country] => DE - Germany [state] => Hessen [town] => Erzhausen )
       //Get an array with geoip-infodata
       function geoCheckIP($ip)
       {
               //check, if the provided ip is valid
               if(!filter_var($ip, FILTER_VALIDATE_IP))
               {
                       throw new InvalidArgumentException("IP is not valid");
               }
               //contact ip-server
               $response=@file_get_contents('http://www.netip.de/search?query='.$ip);
               if (empty($response))
               {
                       throw new InvalidArgumentException("Error contacting Geo-IP-Server");
               }
               //Array containing all regex-patterns necessary to extract ip-geoinfo from page
               $patterns=array();
               $patterns["domain"]  = '#Domain: (.*?) #i';
               $patterns["country"] = '#Country: (.*?) #i';
               $patterns["state"]   = '#State/Region: (.*?)<br#i';
               $patterns["town"]    = '#City: (.*?)<br#i';
               //Array where results will be stored
               $ipInfo=array();
               //check response from ipserver for above patterns
               foreach ($patterns as $key => $pattern)
               {
                       //store the result in array
                       $ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';
               }
               return $ipInfo; }
          print_r(geoCheckIP($ip));
$geoData = geoCheckIP($ip);
$domain=$geoData["domain"];
$country=$geoData["country"];
$state=$geoData["state"];
$town=$geoData["town"];

这个代码我得到了IP

   function chkip() {
    $ipaddress = "";
    $proxy = "";
    if (isset($_SERVER)) {
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
            $ipaddress = $_SERVER["HTTP_X_FORWARDED_FOR"];
            $proxy = $_SERVER["REMOTE_ADDR"];
        } elseif (isset($_SERVER["HTTP_CLIENT_IP"])) {
            $ipaddress = $_SERVER["HTTP_CLIENT_IP"];
        } else {
            $ipaddress = $_SERVER["REMOTE_ADDR"];
        }
    } else {
        if ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) {
            $ipaddress = getenv( 'HTTP_X_FORWARDED_FOR');
            $proxy = $_SERVER["REMOTE_ADDR"];
        } elseif ( getenv( 'HTTP_CLIENT_IP' ) ) {
            $ipaddress = getenv( 'HTTP_CLIENT_IP' );
        } else {
            $ipaddress = getenv( 'REMOTE_ADDR' );
        }
    }
    if (strstr($ipaddress, ',')) {
        $ips = explode(',', $ipaddress);
        $ipaddress = $ips[0];
    }
    if ($proxy != '') {
        $ipaddress = $ipaddress . '(Proxy: '.$proxy.')';
    }
    return $ipaddress;
}  $ipaddress=chkip();

0 个答案:

没有答案