geoip邮政编码查询 [英] geoip zip code query
问题描述
我已经从 http://www.maxmind.com/app/geolitecountry.我将该数据导入到我的数据库中,如下表所示:
I have downloaded the csv geoip lite from http://www.maxmind.com/app/geolitecountry. I imported that data into my db as the following tables:
块:启动IP,结束IP,基因.地点:locid,国家,地区,城市,邮政编码,纬度,长,遇见,区号.
Blocks: startIP, endIP, locid. Location: locid, country, region, city, postalcode, lat, long, met, areacode.
创建IPnum的代码是:
the code that creates the IPnum is:
<? $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;
?>
那么我的查询是:
SELECT postalcode FROM location WHERE locid =(SELECT locid FROM blocks WHERE startIP <= '$ipnum' AND endIP>= '$ipnum' LIMIT 1)
对于 69.63.184.142 的 IP,ipnum 等于 1161803918.db 确实返回了一个查询,但是,该位置来自澳大利亚,而该 ip 绝对不在澳大利亚.
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.
那些熟悉geoip的人,就公式而言,是我做错了什么吗?
those who are familiar with geoip, is it something I am doing wrong as far as formula goes?
推荐答案
这是我使用的方案.如果它没有得到一个 GET 变量,它就会使用用户的远程地址.
Here's one that I use. If it doesn't get a GET var it uses the user's remote address.
<?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>";
?>
这篇关于geoip邮政编码查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!