利用纯真QQIP数据库做快速IP归属地查询_PHP
利用纯真版IP数据文件,优化查询方法,提高格式转换效率,减少读取文件的次数,达到快速在线查询IP的目的。大幅度减少文件读取次数,对保护服务器硬盘应该有一定的好处。并增加一个数据库浏览器,可以浏览纯真版QQIP数据文件。
1.用ip2long()做IP校验
2.用sprintf做强制转换
3.用unpack分析二进制数据,减少了 文件读取次数
4.substr strpos分析字符串,这也减少了文件读取次数
5.可惜速度没有明显提高;)
IP Address:Submit
define('IPDATA','ipdata.db');
//get ip
$ip = isset($_GET['ip'])?$_GET['ip']:getenv("REMOTE_ADDR");
echo "IP: ARIN";
if (-1 === ($ip = ip2long($ip))) die("Invalid IP");
$ip = sprintf("%u",$ip);
$country = $city = '';
echo "
Record No:".Whois($ip,$country,$city);
echo "
Location: []";
function Whois($ip,&$country,&$city)
{
//open ip-database
if (NULL == ($fp = fopen(IPDATA, "rb"))) die("cannot open dbfile.");
//get data section offset
$offset = unpack("Vbegin/Vend", fread($fp, 8));
$begin = 0;$end = ($offset['end'] - $offset['begin']) / 7;
//seek index
while ($begin $num = ($begin + $end) >> 1;
fseek ($fp , ($offset['begin'] + $num * 7) , SEEK_SET ) ;
$record = unpack("Vip", fread($fp, 4));
$record['ip'] = sprintf("%u",$record['ip']);
if ($ip == $record['ip']) { $begin = $num; break;}
if ($ip > $record['ip']) $begin = $num;
else $end = $num;
}
//read index
$record = unpack("Vbeginip/Vloc", ReadBinary($fp,$offset['begin']+$begin*7) );
$locoff =($record['loc'] & 0x00FFFFFF);
$info = unpack("Vendip/Vloc",ReadBinary($fp,$locoff)) ;
$info['endip'] = sprintf("%u",$info['endip']);
if($info['endip'] //get GEO
$countryoff = (($info['loc'] & 0x000000FF)== 1)?($info['loc'] >> 8):($locoff+4);
$country = ReadGEOStr($fp,$countryoff);
$city = ReadGEOStr($fp,$countryoff);
return $begin;
}
function ReadGEOStr($fp,&$offset)
{
$binarydata = ReadBinary($fp,$offset);
$info = unpack("Vloc",$binarydata);
if( ($info['loc'] & 0x000000FF) == 2 ) {
$GeoStr = ReadBinary($fp,$info['loc'] >> 8 );
$GeoStr = substr($GeoStr,0,strpos($GeoStr, 0));
$offset += 4;
}else {
$len = strpos($binarydata, 0);
$GeoStr = substr($binarydata,0,$len);
$offset += $len + 1;
}
return $GeoStr;
}
function ReadBinary($fp,$positionz)
{
fseek ( $fp , $positionz , SEEK_SET ) ;
return fread ( $fp , 32 ) ;
}
?>
===================================
2005-02-22
cnss格式QQ数据库
集成PHP浏览查询器
集成官方whois查询 http://www.mydot.org/t539.html
define('IPDATA','ipdata.db');
$fp = NULL;
if( isset($_GET['ip']) )
{
$ipx = $_GET['ip'];
if (-1 === ($ip = ip2long($ipx))) die("Invalid IP");
echo "IP: ARIN";
$ip = sprintf("%u",$ip);
$ippostion = '';
$start_time=get_time();
echo "
Record No:".Whois($ippostion);
$end_time=get_time();
echo "
Location:";
echo "
Process Time:".round($end_time-$start_time,3)."seconds";
}
else if ( isset($_GET['s']) )
{
$first = $_GET['s'];
$rcount = $_GET['c'];
$startoff = $end = 0;
GetSection($startoff,$end);
if( ($i = $first+$rcount)>$end || $first die ("count overflow");
else
{
$j = $first-$rcount;
echo "Prev:::";
$j = $first+$rcount;
echo "Next
";
}
while( $first {
echo ReadRecord($startoff,$first,$startip,$endip);
$startip = long2ip($startip);
$endip = long2ip($endip);
echo " :--
";
$first ++;
}
fclose($fp);
}
else
{
echo "Powered by upsdn.net";
echo "
Thanks to cz88.net";
}
function Whois(&$country)
{
global $ip,$fp;
$startoff = $begin = $end = 0;
GetSection($startoff,$end);
//seek index
while ($begin $num = ($begin + $end) >> 1;
fseek ($fp , $startoff + $num * 7 , SEEK_SET ) ;
$record = unpack("Vip", fread($fp, 4));
$record['ip'] = sprintf("%u",$record['ip']);
if ($ip == $record['ip']) { $begin = $num; break;}
if ($ip > $record['ip']) $begin = $num;
else $end = $num;
}
//read index
$startip = $endip = 0;
$country = ReadRecord($startoff,$begin,$startip,$endip);
fclose($fp);
return $begin;
}
function GetSection(&$startoff,&$total)
{
global $fp;
//open ip-database
if (NULL == ($fp = fopen(IPDATA, "rb"))) die("cannot open dbfile.");
//get data section offset
$offset = unpack("Vbegin/Vend", fread($fp, 8));
$total = ($offset['end'] - $offset['begin']) / 7;
$startoff = $offset['begin'];
}
function ReadRecord($startoff,$number,&$startip,&$endip)
{
global $ip;
$record = unpack("Vbeginip/Vloc", ReadBinary($startoff+$number*7) );
$startip = $record['beginip'];
$locoff =($record['loc'] & 0x00FFFFFF);
$info = unpack("Vendip/Vloc",ReadBinary($locoff)) ;
$endip = $info['endip'];
$info['endip'] = sprintf("%u",$info['endip']);
if($info['endip'] //get GEO
$countryoff = (($info['loc'] & 0x000000FF)== 1)?($info['loc'] >> 8):($locoff+4);
return ReadGEOStr($countryoff)."->".ReadGEOStr($countryoff);
}
function ReadGEOStr(&$offset)
{
$binarydata = ReadBinary($offset);
$info = unpack("Vloc",$binarydata);
if( ($info['loc'] & 0x000000FF) == 2 ) {
$GeoStr = ReadBinary($info['loc'] >> 8 );
$GeoStr = substr($GeoStr,0,strpos($GeoStr, 0));
$offset += 4;
}else {
$len = strpos($binarydata, 0);
$GeoStr = substr($binarydata,0,$len);
$offset += $len + 1;
}
return $GeoStr;
}
function ReadBinary($positionz)
{
global $fp;
fseek ( $fp , $positionz , SEEK_SET ) ;
return fread ( $fp , 32 ) ;
}
function get_time() {
$mtime=microtime();
$mtime=explode(" ",$mtime);
$mtime=$mtime[1]+$mtime[0];
return($mtime);
}
?>
纯真版IP数据库下载:http://www.cz88.net/ip/

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

12306订票app下载最新版是一款大家非常满意的出行购票软件,想去哪里就去那里非常方便,软件内提供的票源非常多,只需要通过实名认证就能在线购票,所有用户的出行车票机票都可以轻松买到,享受不同的优惠折扣。还能提前开启预约抢票,预约酒店、专车接送都是可以的,有了它想去哪里就去那里一键购票,出行更加简单方便,让大家的出行体验更舒服,现在小编在线详细为12306用户们带来查看历史购票记录的方法。 1.打开铁路12306,点击右下角我的,点击我的订单 2.在订单页面点击已支付。 3.在已支付页

学信网如何查询自己的学历?在学信网中是可以查询到自己的学历,很多用户都不知道如何在学信网中查询到自己的学历,接下来就是小编为用户带来的学信网查询自己学历方法图文教程,感兴趣的用户快来一起看看吧!学信网使用教程学信网如何查询自己的学历一、学信网入口:https://www.chsi.com.cn/二、网站查询:第一步:点击上方学信网地址,进入首页点击【学历查询】;第二步:在最新的网页中点击如下图箭头所示的【查询】;第三步:之后在新页面点击【的登陆学信档案】;第四步:在登陆页面输入信息点击【登陆】;

Win11系统中“我的电脑”路径有何不同?快速查找方法!随着Windows系统的不断更新,最新的Windows11系统也带来了一些新的变化和功能。其中一个常见的问题是用户在Win11系统中找不到“我的电脑”的路径,这在之前的Windows系统中通常是很简单的操作。本文将介绍Win11系统中“我的电脑”的路径有何不同,以及快速查找的方法。在Windows1

苹果公司最新发布的iOS18、iPadOS18以及macOSSequoia系统为Photos应用增添了一项重要功能,旨在帮助用户轻松恢复因各种原因丢失或损坏的照片和视频。这项新功能在Photos应用的"工具"部分引入了一个名为"已恢复"的相册,当用户设备中存在未纳入其照片库的图片或视频时,该相册将自动显示。"已恢复"相册的出现为因数据库损坏、相机应用未正确保存至照片库或第三方应用管理照片库时照片和视频丢失提供了解决方案。用户只需简单几步

Hibernate多态映射可映射继承类到数据库,提供以下映射类型:joined-subclass:为子类创建单独表,包含父类所有列。table-per-class:为子类创建单独表,仅包含子类特有列。union-subclass:类似joined-subclass,但父类表联合所有子类列。

HTML无法直接读取数据库,但可以通过JavaScript和AJAX实现。其步骤包括建立数据库连接、发送查询、处理响应和更新页面。本文提供了利用JavaScript、AJAX和PHP来从MySQL数据库读取数据的实战示例,展示了如何在HTML页面中动态显示查询结果。该示例使用XMLHttpRequest建立数据库连接,发送查询并处理响应,从而将数据填充到页面元素中,实现了HTML读取数据库的功能。

如何在PHP中使用MySQLi建立数据库连接:包含MySQLi扩展(require_once)创建连接函数(functionconnect_to_db)调用连接函数($conn=connect_to_db())执行查询($result=$conn->query())关闭连接($conn->close())

PHP中处理数据库连接报错,可以使用以下步骤:使用mysqli_connect_errno()获取错误代码。使用mysqli_connect_error()获取错误消息。通过捕获并记录这些错误信息,可以轻松识别并解决数据库连接问题,确保应用程序的顺畅运行。
