当前位置:网站首页>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
边栏推荐
- BGP actual network configuration
- The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
- The left-hand side of an assignment expression may not be an optional property access.ts(2779)
- 通讯协议设计与实现
- Attack and defense world ----- summary of web knowledge points
- SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
- 【PyTorch实战】图像描述——让神经网络看图讲故事
- Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
- 【统计学习方法】学习笔记——支持向量机(上)
- SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
猜你喜欢
The road to success in R & D efficiency of 1000 person Internet companies
MPLS experiment
OSPF exercise Report
How to use PS link layer and shortcut keys, and how to do PS layer link
2022聚合工艺考试题模拟考试题库及在线模拟考试
Zhimei creative website exercise
SQL head injection -- injection principle and essence
30. Feed shot named entity recognition with self describing networks reading notes
【统计学习方法】学习笔记——提升方法
Epp+dis learning path (1) -- Hello world!
随机推荐
visual stdio 2017关于opencv4.1的环境配置
[statistical learning methods] learning notes - Chapter 5: Decision Tree
【从 0 开始学微服务】【00】课程概述
EPP+DIS学习之路(2)——Blink!闪烁!
TypeScript 接口继承
Configure an encrypted web server
Niuke website
Cookie
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
Realize a simple version of array by yourself from
Sorting, dichotomy
Epp+dis learning road (2) -- blink! twinkle!
[爬虫]使用selenium时,躲避脚本检测
On valuation model (II): PE index II - PE band
Solutions to cross domain problems
平安证券手机行开户安全吗?
2022危险化学品生产单位安全生产管理人员考题及在线模拟考试
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
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)
Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios