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

蘋果公司最新發布的iOS18、iPadOS18以及macOSSequoia系統為Photos應用程式增添了一項重要功能,旨在幫助用戶輕鬆恢復因各種原因遺失或損壞的照片和影片。這項新功能在Photos應用的"工具"部分引入了一個名為"已恢復"的相冊,當用戶設備中存在未納入其照片庫的圖片或影片時,該相冊將自動顯示。 "已恢復"相簿的出現為因資料庫損壞、相機應用未正確保存至照片庫或第三方應用管理照片庫時照片和視頻丟失提供了解決方案。使用者只需簡單幾步

Win11系統中「我的電腦」路徑有何不同?快速找方法!隨著Windows系統的不斷更新,最新的Windows11系統也帶來了一些新的變化和功能。其中一個常見的問題是使用者在Win11系統中找不到「我的電腦」的路徑,這在先前的Windows系統中通常是很簡單的操作。本文將介紹Win11系統中「我的電腦」的路徑有何不同,以及快速尋找的方法。在Windows1

Hibernate多態映射可映射繼承類別到資料庫,提供以下映射類型:joined-subclass:為子類別建立單獨表,包含父類別所有欄位。 table-per-class:為子類別建立單獨資料表,僅包含子類別特有列。 union-subclass:類似joined-subclass,但父類別表聯合所有子類別列。

如何在PHP中使用MySQLi建立資料庫連線:包含MySQLi擴充(require_once)建立連線函數(functionconnect_to_db)呼叫連線函數($conn=connect_to_db())執行查詢($result=$conn->query())關閉連線( $conn->close())

PHP處理資料庫連線報錯,可以使用下列步驟:使用mysqli_connect_errno()取得錯誤代碼。使用mysqli_connect_error()取得錯誤訊息。透過擷取並記錄這些錯誤訊息,可以輕鬆識別並解決資料庫連接問題,確保應用程式的順暢運作。

HTML無法直接讀取資料庫,但可以透過JavaScript和AJAX實作。其步驟包括建立資料庫連線、發送查詢、處理回應和更新頁面。本文提供了利用JavaScript、AJAX和PHP來從MySQL資料庫讀取資料的實戰範例,展示如何在HTML頁面中動態顯示查詢結果。此範例使用XMLHttpRequest建立資料庫連接,發送查詢並處理回應,從而將資料填入頁面元素中,實現了HTML讀取資料庫的功能。

透過Go標準庫database/sql包,可以連接到MySQL、PostgreSQL或SQLite等遠端資料庫:建立包含資料庫連接資訊的連接字串。使用sql.Open()函數開啟資料庫連線。執行SQL查詢和插入操作等資料庫操作。使用defer關閉資料庫連線以釋放資源。

在Golang中使用資料庫回呼函數可以實現:在指定資料庫操作完成後執行自訂程式碼。透過單獨的函數新增自訂行為,無需編寫額外程式碼。回調函數可用於插入、更新、刪除和查詢操作。必須使用sql.Exec、sql.QueryRow或sql.Query函數才能使用回呼函數。
