=0){ window.location.href="https:/..." />

根据IP跳转不同城市代码(修正版)

  • 发表于
  • 前端

根据IP跳转不同城市PHP代码(修正版)



$useripNum || $userip2num<$useripNum) {
$Middle= intval(($EndNum + $BeginNum) / 2);

//偏移指针到索引位置读取4个字节
fseek($fd, $useripbegin + 7 * $Middle);
$useripData1 = fread($fd, 4);
if(strlen($useripData1) < 4) {
fclose($fd);
return 'File Error';
}
//提取出来的数据转换成长整形,如果数据是负数则加上2的32次幂
$userip1num = implode('', unpack('L', $useripData1));
if($userip1num < 0) $userip1num += pow(2, 32);

//提取的长整型数大于我们IP地址则修改结束位置进行下一次循环
if($userip1num > $useripNum) {
$EndNum = $Middle;
continue;
}

//取完上一个索引后取下一个索引
$DataSeek = fread($fd, 3);
if(strlen($DataSeek) < 3) {
fclose($fd);
return 'File Error';
}
$DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
fseek($fd, $DataSeek);
$useripData2 = fread($fd, 4);
if(strlen($useripData2) < 4) {
fclose($fd);
return 'File Error';
}
$userip2num = implode('', unpack('L', $useripData2));
if($userip2num < 0) $userip2num += pow(2, 32);

//找不到IP地址对应城市
if($userip2num < $useripNum) {
if($Middle == $BeginNum) {
fclose($fd);
return 'No Data';
}
$BeginNum = $Middle;
}
}

$useripFlag = fread($fd, 1);
if($useripFlag == chr(1)) {
$useripSeek = fread($fd, 3);
if(strlen($useripSeek) < 3) {
fclose($fd);
return 'System Error';
}
$useripSeek = implode('', unpack('L', $useripSeek.chr(0)));
fseek($fd, $useripSeek);
$useripFlag = fread($fd, 1);
}

if($useripFlag == chr(2)) {
$AddrSeek = fread($fd, 3);
if(strlen($AddrSeek) < 3) {
fclose($fd);
return 'System Error';
}
$useripFlag = fread($fd, 1);
if($useripFlag == chr(2)) {
$AddrSeek2 = fread($fd, 3);
if(strlen($AddrSeek2) < 3) {
fclose($fd);
return 'System Error';
}
$AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
fseek($fd, $AddrSeek2);
} else {
fseek($fd, -1, SEEK_CUR);
}

while(($char = fread($fd, 1)) != chr(0))
$useripAddr2 .= $char;

$AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
fseek($fd, $AddrSeek);

while(($char = fread($fd, 1)) != chr(0))
$useripAddr1 .= $char;
} else {
fseek($fd, -1, SEEK_CUR);
while(($char = fread($fd, 1)) != chr(0))
$useripAddr1 .= $char;

$useripFlag = fread($fd, 1);
if($useripFlag == chr(2)) {
$AddrSeek2 = fread($fd, 3);
if(strlen($AddrSeek2) < 3) {
fclose($fd);
return 'System Error';
}
$AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
fseek($fd, $AddrSeek2);
} else {
fseek($fd, -1, SEEK_CUR);
}
while(($char = fread($fd, 1)) != chr(0)){
$useripAddr2 .= $char;
}
}
fclose($fd);

//返回IP地址对应的城市结果
if(preg_match('/http/i', $useripAddr2)) {
$useripAddr2 = '';
}
$useripaddr = "$useripAddr1 $useripAddr2";
$useripaddr = preg_replace('/CZ88.Net/is', '', $useripaddr);
$useripaddr = preg_replace('/^s*/is', '', $useripaddr);
$useripaddr = preg_replace('/s*$/is', '', $useripaddr);
if(preg_match('/http/i', $useripaddr) || $useripaddr == '') {
$useripaddr = 'No Data';
}

return $useripaddr;
}
?>


用法:
1.下载纯真IP数据库 下载地址 http://update.cz88.net/soft/qqwry.rar 用迅雷下
我们只用里面的QQWry.Dat,我命名成了IP.Dat
2.把我发的代码存为ip.php
3.把1跟2的文件均放在同一个文件夹里,比如你放在文件夹 /ipcity/
4.在需要的页面插入

补充:关于这个代码在网上很多,但基本都说有错误,我发后也有人找我说有错,后经检查确实有问题;为了方便各位使用,我又全部检查修正

使用本代码必须放在网络上,本机测试无效!