这些IP与城市和分公司首页的对应关系应该保存到文本文件或数据库中
分公司打开网站时,根据$_SERVER['REMOTE_ADDR']得到用户IP,
假设该对应关系是保存在数据库中
ip city branch_page211.98.2.4 沈阳 shenyang.htm
............
再根据IP从对应关系中找到城市和分公司首页,
select branch_page from tbl where ip = {$_SERVER['REMOTE_ADDR']}
转向
header("Location: $branch_page");
分公司打开网站时,根据$_SERVER['REMOTE_ADDR']得到用户IP,
假设该对应关系是保存在数据库中
ip city branch_page211.98.2.4 沈阳 shenyang.htm
............
再根据IP从对应关系中找到城市和分公司首页,
select branch_page from tbl where ip = {$_SERVER['REMOTE_ADDR']}
转向
header("Location: $branch_page");
而且,他里面的数据都是按段来分的(也有点的),这个怎么判断才能效率最高啊,
比如现在有一个61.133.133.133访问,数据库里有对应的一段地址是61.133.0.0-61.133.255.255是某地的,我现在怎么判断才能实现最高效率的检索IP所对应的地市然后转到相应页面呢?
用PHP
(
city_info varchar(50) not null,
ip_begin1 int,
ip_begin2 int,
ip_begin3 int,
ip_begin4 int,
ip_end1 int,
ip_end2 int,
ip_end3 int,
ip_end4 int
)
写ip的时候分解成4段写入.
这样的表结构看起来虽然有些弱智,但是方便查询语句操作也能满足你的要求了,
你用php写ip地址段查询语句的时候,可以用pos()分解当前ip地址,然后构造下面的语句:select city_info from ip_seg
where $ip_1 >= ip_begin1 and $ip_1 <= ip_end1 and
$ip_2 >= ip_begin2 and $ip_2 <= ip_end2 and
$ip_3 >= ip_begin3 and $ip_3 <= ip_end3 and
$ip_4 >= ip_begin4 and $ip_4 <= ip_end4
QQ那个我也不知道怎么打开
create table ip_seg
(
city_info varchar(50) not null,
ip_begin varchar(12),
ip_end varchar(12)
)ip_begin,ip_end的格式是ip各个段的合并,每个段不够3位的补0
比如:127.0.0.1 就是 127000000001将$_SERVER['REMOTE_ADDR']也分解成这样的格式
那么就可以用这个语句查询select * from ip_seg where $ip >= ip_begin and $ip <= ip_end
* Beginning IP
* Ending IP
* Beginning IP Number
* Ending IP Number
* Country Code
* Country Name
-------------------------------------------------------------------
"2.6.190.56","2.6.190.63","33996344","33996351","GB","United Kingdom"
"3.0.0.0","4.17.142.255","50331648","68259583","US","United States"
"4.17.143.0","4.17.143.15","68259584","68259599","CA","Canada"
"4.17.143.16","4.18.32.71","68259600","68296775","US","United States"
"4.18.32.72","4.18.32.79","68296776","68296783","MX","Mexico"
"4.18.32.80","4.18.65.255","68296784","68305407","US","United States"
"4.18.66.0","4.18.67.255","68305408","68305919","MX","Mexico"
"4.18.68.0","4.20.69.255","68305920","68437503","US","United States"
"4.20.70.0","4.20.70.255","68437504","68437759","FR","France"
"4.20.71.0","4.21.164.183","68437760","68527287","US","United States"
"4.21.164.184","4.21.164.191","68527288","68527295","CA","Canada"
"4.21.164.192","4.22.196.255","68527296","68601087","US","United States"
"4.22.197.0","4.22.197.255","68601088","68601343","CA","Canada"
-----------------------------------------------------------------
假设你有以上的信息,具体到了某个城市,
然后通过:
function getIpInformation($ip)
{
global $class_db;
if(empty($ip) || ! isset($ip))
{
Return '';
}
$iparray = explode(".", $ip);
$iprange = 16777216 * $iparray[0] + 65536 * $iparray[1] + 256 * $iparray[2] + $iparray[3];
$ip_sql = "SELECT countryname FROM " . TABLE_IPADDRESS . " ip WHERE " . $iprange . " >= ip.begin_num AND " . $iprange . " <= ip.end_num";
$ip_result = $class_db->query($ip_sql);
$ip_row = $ip_result->fetchRow();
$ip_country = $ip_row['countryname'];
Return $ip_country;
}求个对应的城市名称,
再通过一个表,将此城市与分公司对应起来,跳转OK