当前位置:网站首页>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
边栏推荐
- leetcode刷题:二叉树26(二叉搜索树中的插入操作)
- In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
- Decrypt gd32 MCU product family, how to choose the development board?
- H3C HCl MPLS layer 2 dedicated line experiment
- leetcode刷题:二叉树20(二叉搜索树中的搜索)
- Solutions to cross domain problems
- Epp+dis learning road (2) -- blink! twinkle!
- The left-hand side of an assignment expression may not be an optional property access. ts(2779)
- Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
- Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being
猜你喜欢
On valuation model (II): PE index II - PE band
Connect to blog method, overload, recursion
Day-16 set
OSPF exercise Report
【统计学习方法】学习笔记——支持向量机(下)
Tutorial on principles and applications of database system (007) -- related concepts of database
"Series after reading" my God! It's so simple to understand throttling and anti shake~
[pytorch practice] use pytorch to realize image style migration based on neural network
Multi row and multi column flex layout
How to use PS link layer and shortcut keys, and how to do PS layer link
随机推荐
Realize a simple version of array by yourself from
SQL lab 1~10 summary (subsequent continuous update)
Decrypt gd32 MCU product family, how to choose the development board?
【深度学习】图像多标签分类任务,百度PaddleClas
【二叉树】删点成林
Cryptography series: detailed explanation of online certificate status protocol OCSP
【PyTorch实战】图像描述——让神经网络看图讲故事
[Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
【从 0 开始学微服务】【02】从单体应用走向服务化
【统计学习方法】学习笔记——第四章:朴素贝叶斯法
广州市召开安全生产工作会议
静态Vxlan 配置
Object. Simple implementation of assign()
Static vxlan configuration
Using stack to convert binary to decimal
编译 libssl 报错
2022广东省安全员A证第三批(主要负责人)考试练习题及模拟考试
Niuke website
visual stdio 2017关于opencv4.1的环境配置
Attack and defense world ----- summary of web knowledge points