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

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool