当前位置:网站首页>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
边栏推荐
- Day-17 connection set
- H3C HCl MPLS layer 2 dedicated line experiment
- leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
- [疑难杂症]pip运行突然出现ModuleNotFoundError: No module named ‘pip‘
- 对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
- [statistical learning methods] learning notes - improvement methods
- 【统计学习方法】学习笔记——提升方法
- Epp+dis learning road (2) -- blink! twinkle!
- Aike AI frontier promotion (7.7)
- Idea 2021 Chinese garbled code
猜你喜欢

SQL Lab (46~53) (continuous update later) order by injection

Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being

leetcode刷题:二叉树22(二叉搜索树的最小绝对差)

OSPF exercise Report

30. Feed shot named entity recognition with self describing networks reading notes
![[statistical learning methods] learning notes - Chapter 5: Decision Tree](/img/0e/c60e04ab4a7ae4728cc76eff1c028a.png)
[statistical learning methods] learning notes - Chapter 5: Decision Tree

SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)

Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment

Epp+dis learning path (1) -- Hello world!

How to use PS link layer and shortcut keys, and how to do PS layer link
随机推荐
EPP+DIS学习之路(1)——Hello world!
gcc 编译报错
Realize all, race, allsettled and any of the simple version of promise by yourself
免备案服务器会影响网站排名和权重吗?
Is it safe to open an account in Ping An Securities mobile bank?
Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)
About web content security policy directive some test cases specified through meta elements
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
[statistical learning methods] learning notes - Chapter 5: Decision Tree
解密GD32 MCU产品家族,开发板该怎么选?
Simple implementation of call, bind and apply
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
Charles: four ways to modify the input parameters or return results of the interface
(待会删)yyds,付费搞来的学术资源,请低调使用!
Customize the web service configuration file
【统计学习方法】学习笔记——支持向量机(上)
On valuation model (II): PE index II - PE band
TypeScript 接口继承
ENSP MPLS layer 3 dedicated line
【PyTorch实战】图像描述——让神经网络看图讲故事