利用纯真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/

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

What is the difference in the "My Computer" path in Win11? Quick way to find it! As the Windows system is constantly updated, the latest Windows 11 system also brings some new changes and functions. One of the common problems is that users cannot find the path to "My Computer" in Win11 system. This was usually a simple operation in previous Windows systems. This article will introduce how the paths of "My Computer" are different in Win11 system, and how to quickly find them. In Windows1

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.
