简体   繁体   English

geoip邮政编码查询

[英]geoip zip code query

I have downloaded the csv geoip lite from http://www.maxmind.com/app/geolitecountry . 我已经从http://www.maxmind.com/app/geolitecountry下载了csv geoip lite。 I imported that data into my db as the following tables: 我将这些数据作为下表导入到我的数据库中:

Blocks: startIP, endIP, locid. 块:startIP,endIP,locid。 Location: locid, country, region, city, postalcode, lat, long, met, areacode. 位置:位置,国家,地区,城市,邮政编码,纬度,经度,地区,区域代码。

the code that creates the IPnum is: 创建IPnum的代码是:

<? $ip =$_SERVER['REMOTE_ADDR'];


        list($w, $x, $y, $z) = explode('.', $ip);

        $one = 16777216* $w;
        $two = 65536* $x ;
        $three = 256*$y;

        $ipnum = $one + $two+ $three + $z;
?>

then my query is: 那么我的查询是:

SELECT postalcode FROM location WHERE locid =(SELECT locid FROM blocks WHERE startIP <= '$ipnum' AND endIP>= '$ipnum' LIMIT 1)

for a IP of 69.63.184.142, the ipnum is equal to 1161803918. the db does return a query, however, the location is from Australia, and that ip is definitely not in Australia. 对于69.63.184.142的IP,ipnum等于1161803918。数据库确实返回查询,但是该位置来自澳大利亚,并且该ip绝对不在澳大利亚。

those who are familiar with geoip, is it something I am doing wrong as far as formula goes? 那些熟悉geoip的人,就公式而言,这是我做错了吗?

Here's one that I use. 这是我用的一个。 If it doesn't get a GET var it uses the user's remote address. 如果没有得到GET var,它将使用用户的远程地址。

<?php
if(!empty($_GET['ip'])){
if(preg_match('!^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$!',$_GET['ip'])){
    $ip_address=$_GET['ip'];    
} else {
    print ("invalid ip address");   
}
} else {
$ip_address=$_SERVER['REMOTE_ADDR'];    
}
$test = file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip_address);
$test = unserialize($test);
print "<html><pre>";
print_r ($test);
print "</pre></html>";
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM