当前位置:网站首页>PHP调用纯真IP数据库返回具体地址
PHP调用纯真IP数据库返回具体地址
2022-07-07 10:36:00 【全栈程序员站长】
function convertip($ip) {
$ip1num = 0;
$ip2num = 0;
$ipAddr1 ="";
$ipAddr2 ="";
$dat_path = './QQWry.Dat';
if(!preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ip)) {
return 'IP Address Error';
}
if(!$fd = @fopen($dat_path, 'rb')){
return 'IP date file not exists or access denied';
}
$ip = explode('.', $ip);
$ipNum = $ip[0] * 16777216 + $ip[1] * 65536 + $ip[2] * 256 + $ip[3];
$DataBegin = fread($fd, 4);
$DataEnd = fread($fd, 4);
$ipbegin = implode('', unpack('L', $DataBegin));
if($ipbegin < 0) $ipbegin += pow(2, 32);
$ipend = implode('', unpack('L', $DataEnd));
if($ipend < 0) $ipend += pow(2, 32);
$ipAllNum = ($ipend - $ipbegin) / 7 + 1;
$BeginNum = 0;
$EndNum = $ipAllNum;
while($ip1num>$ipNum || $ip2num<$ipNum) {
$Middle= intval(($EndNum + $BeginNum) / 2);
fseek($fd, $ipbegin + 7 * $Middle);
$ipData1 = fread($fd, 4);
if(strlen($ipData1) < 4) {
fclose($fd);
return 'System Error';
}
$ip1num = implode('', unpack('L', $ipData1));
if($ip1num < 0) $ip1num += pow(2, 32);
if($ip1num > $ipNum) {
$EndNum = $Middle;
continue;
}
$DataSeek = fread($fd, 3);
if(strlen($DataSeek) < 3) {
fclose($fd);
return 'System Error';
}
$DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
fseek($fd, $DataSeek);
$ipData2 = fread($fd, 4);
if(strlen($ipData2) < 4) {
fclose($fd);
return 'System Error';
}
$ip2num = implode('', unpack('L', $ipData2));
if($ip2num < 0) $ip2num += pow(2, 32);
if($ip2num < $ipNum) {
if($Middle == $BeginNum) {
fclose($fd);
return 'Unknown';
}
$BeginNum = $Middle;
}
}
$ipFlag = fread($fd, 1);
if($ipFlag == chr(1)) {
$ipSeek = fread($fd, 3);
if(strlen($ipSeek) < 3) {
fclose($fd);
return 'System Error';
}
$ipSeek = implode('', unpack('L', $ipSeek.chr(0)));
fseek($fd, $ipSeek);
$ipFlag = fread($fd, 1);
}
if($ipFlag == chr(2)) {
$AddrSeek = fread($fd, 3);
if(strlen($AddrSeek) < 3) {
fclose($fd);
return 'System Error';
}
$ipFlag = fread($fd, 1);
if($ipFlag == 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))
$ipAddr2 .= $char;
$AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
fseek($fd, $AddrSeek);
while(($char = fread($fd, 1)) != chr(0))
$ipAddr1 .= $char;
} else {
fseek($fd, -1, SEEK_CUR);
while(($char = fread($fd, 1)) != chr(0))
$ipAddr1 .= $char;
$ipFlag = fread($fd, 1);
if($ipFlag == 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)){
$ipAddr2 .= $char;
}
}
fclose($fd);
if(preg_match('/http/i', $ipAddr2)) {
$ipAddr2 = '';
}
$ipaddr = "$ipAddr1 $ipAddr2";
$ipaddr = preg_replace('/CZ88.NET/is', '', $ipaddr);
$ipaddr = preg_replace('/^s*/is', '', $ipaddr);
$ipaddr = preg_replace('/s*$/is', '', $ipaddr);
if(preg_match('/http/i', $ipaddr) || $ipaddr == '') {
$ipaddr = 'Unknown';
}
return $ipaddr;
}
只要调用此方法,传入一个正确的IP地址,就能得到纯真IP数据库中相对应的地理位置了
例如:
convertip(202.96.209.5)
返回的值为:上海市 电信
注:写有此方法的文件需和纯真数据库文件(QQWry.Dat)放在同一级目录,否则就需要修改$dat_path的内容。
准备工作:
建议本地IP地址数据库,请到http://www.cz88.net/这个网站下载一个纯真IP数据库,安装完成后,到安装目录里把QQWry.dat文件取出来,这个就是我们想要的IP数据库了,放到你想要目录下。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/113445.html原文链接:https://javaforall.cn
边栏推荐
- Vxlan 静态集中网关
- Customize the web service configuration file
- leetcode刷题:二叉树27(删除二叉搜索树中的节点)
- SQL Lab (46~53) (continuous update later) order by injection
- leetcode刷题:二叉树23(二叉搜索树中的众数)
- 【统计学习方法】学习笔记——提升方法
- Realize all, race, allsettled and any of the simple version of promise by yourself
- Routing strategy of multi-point republication [Huawei]
- SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
- SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
猜你喜欢
Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
leetcode刷题:二叉树23(二叉搜索树中的众数)
leetcode刷题:二叉树19(合并二叉树)
Several ways to clear floating
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
Static routing assignment of network reachable and telent connections
Day-14 common APIs
File upload vulnerability - upload labs (1~2)
(to be deleted later) yyds, paid academic resources, please keep a low profile!
随机推荐
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
编译 libssl 报错
Solutions to cross domain problems
<No. 9> 1805. Number of different integers in the string (simple)
Typescript interface inheritance
[statistical learning method] learning notes - support vector machine (Part 2)
Idea 2021 Chinese garbled code
[deep learning] image multi label classification task, Baidu paddleclas
Charles: four ways to modify the input parameters or return results of the interface
Simple implementation of call, bind and apply
Common knowledge of one-dimensional array and two-dimensional array
[爬虫]使用selenium时,躲避脚本检测
【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
Experiment with a web server that configures its own content
gcc 编译报错
Utiliser la pile pour convertir le binaire en décimal
SQL blind injection (WEB penetration)
JS to convert array to tree data
[pytorch practice] use pytorch to realize image style migration based on neural network
sql-lab (54-65)