公交车上荫蒂添的好舒服的电影-公用玩物(np双xing总受)-公用小荡货芊芊-公与妇仑乱hd-攻把受做哭边走边肉楼梯play-古装一级淫片a免费播放口

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
版主

PHP讀取IP.dat和共享內存高效檢索

machangmu
2012年8月8日 14:51 本文熱度 4816
<?php
/*
QQwry.dat格式說明如下:
A。文件頭,共8字節
B。若干條記錄的結束地址+國家和區域
C。按照從小到大排列的若干條起始地址+結束地址偏移,定長,7字節
D。所有的IP都是用4字節整數記錄的,并且遵照Intel次序,高位在后,低位在前。
E。所有偏移量都是絕對偏移,就是從文件最開頭計算。
F。除了文件頭用了兩個4字節偏移,其余偏移量都用3字節。
G。所有的偏移量也是低位在前,高位在后
H。采用了一些字符串壓縮技術
  
1。文件頭,共8字節
FirstStartIpOffset:4 第一個起始IP的絕對偏移
LastStartIpOffset:4 最后一個起始IP的絕對偏移
  
2。起始地址+結束地址偏移記錄區
每條記錄7字節,按照起始地址從小到大排列
  
StartIp:4 起始地址,整數形式的IP
EndIpOffset:3 結束地址絕對偏移
  
3。結束地址+國家+區域記錄區
  
EndIP:4
國家+區域記錄:不定長
  
4。國家+區域記錄,有幾種形式
4.1。
國家字符串,以 0×0 結束
區域字符串,以 0×0 結束
  
4.2。
Flag:1 標識取值: 0×1,后面沒有Local記錄
0×2,后面還有Local記錄
sCountryOffset:3 實際的字符串要去這個偏移位置去找
LocalRec:不定長,可選 根據Flag取值而定。這個記錄也類似Country,可能采用壓縮
  
4.3 LocalRec結構一
flag:1 還不是十分了解這個flag含義,取值 0×1 or 0×2
sLocalOffset:3
  
4.4 LocalRec結構二
sLocal:不定長 普通的C風格字符串
  
注意:sCountryOffset指向的位置可能依然是4.2格式的,不知道為什么這樣設計。
  
Flag取0×1時,sCountryOffset指向的位置可能是Flag為0×2,這時,LocalRec也在這里尋找。
  
現在不明白當記錄Local的位置遇到0×2的標志意味著什么。
  
在qqwry.dat中,似乎存在一些錯誤。
個別的記錄Local會被寫為:
0×2,0×0,0×0,0×0
根據規則,應該到文件最開頭去尋找,可是,文件最開頭顯然不是記錄這些的。
*/
  
$qqwry_root_path ="";
define('QQWRY' , $qqwry_root_path . 'tinyipdata.dat' );
  
function IpToInt($Ip) {
     $array=explode('.',$Ip);
     $Int=($array[0] * 256*256*256) + ($array[1]*256*256) + ($array[2]*256) + $array[3];
     return $Int;
}
  
function IntToIp($Int) {
     $b1=($Int & 0xff000000)>>24;
     if ($b1<0) $b1+=0x100;
     $b2=($Int & 0x00ff0000)>>16;
     if ($b2<0) $b2+=0x100;
     $b3=($Int & 0x0000ff00)>>8;
     if ($b3<0) $b3+=0x100;
     $b4= $Int & 0x000000ff;
     if ($b4<0) $b4+=0x100;
     $Ip=$b1.'.'.$b2.'.'.$b3.'.'.$b4;
     return $Ip;
}
  
  
class TQQwry
{
     var $StartIP = 0;
     var $EndIP    = 0;
     var $Country = '';
     var $Local    = '';
  
     var $CountryFlag = 0; // 標識 Country位置
                           // 0x01,隨后3字節為Country偏移,沒有Local
                           // 0x02,隨后3字節為Country偏移,接著是Local
                           // 其他,Country,Local,Local有類似的壓縮。可能多重引用。
     var $fp;
  
     var $FirstStartIp = 0;
     var $LastStartIp = 0;
     var $EndIpOff = 0;
  
     function getStartIp ( $RecNo ) {
         $offset = $this->FirstStartIp + $RecNo * 7;
         @fseek ( $this->fp , $offset , SEEK_SET );
         $buf = fread ( $this->fp , 7 );
         $this->EndIpOff = ord($buf[4]) + (ord($buf[5])*256) + (ord($buf[6])* 256*256);
         $this->StartIp = ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256);
         return $this->StartIp;
     }
  
     function getEndIp ( ) {
         @fseek ( $this->fp , $this->EndIpOff , SEEK_SET );
         $buf = fread ( $this->fp , 5 );
         $this->EndIp = ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256);
         $this->CountryFlag = ord ( $buf[4] );
         return $this->EndIp;
     }
  
     function getCountry() {
  
         switch ( $this->CountryFlag ) {
             case 1:
             case 2:
                 $this->Country = $this->getFlagStr ( $this->EndIpOff+4);
                 //echo sprintf('EndIpOffset=(%x)',$this->EndIpOff );
                 $this->Local = ( 1 == $this->CountryFlag )? '' : $this->getFlagStr ( $this->EndIpOff+8);
                 break;
             default :
                 $this->Country = $this->getFlagStr ($this->EndIpOff+4);
                 $this->Local =    $this->getFlagStr ( ftell ( $this->fp ));
  
         }
     }
  
  
     function getFlagStr ( $offset )
     {
  
         $flag = 0;
         while ( 1 ){
             @fseek ( $this->fp , $offset , SEEK_SET );
             $flag = ord ( fgetc ( $this->fp ) );
             if ( $flag == 1 ││ $flag == 2 ) {
                 $buf = fread ($this->fp , 3 );
                 if ($flag == 2 ){
                     $this->CountryFlag = 2;
                     $this->EndIpOff = $offset - 4;
                 }
                 $offset = ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])* 256*256);
             }else{
                 break;
             }
  
         }
         if ( $offset < 12 )
             return '';
         @fseek($this->fp , $offset , SEEK_SET );
         return $this->getStr();
     }
     function getStr ( )
     {
         $str = '';
         while ( 1 ) {
             $c = fgetc ( $this->fp );
             if ( ord ( $c[0] ) == 0   )
                break;
             $str .= $c;
         }
         return $str;
     }
  
  
     function qqwry ($dotip) {
  
         $nRet="";
         $ip = IpToInt ( $dotip );
  
         $this->fp= @fopen(QQWRY, "rb");
         if ($this->fp == NULL) {
               $szLocal= "OpenFileError";
             return 1;
  
           }
           @fseek ( $this->fp , 0 , SEEK_SET );
         $buf = fread ( $this->fp , 8 );
         $this->FirstStartIp = ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256);
         $this->LastStartIp   = ord($buf[4]) + (ord($buf[5])*256) + (ord($buf[6])*256*256) + (ord($buf[7])*256*256*256);
  
         $RecordCount= floor( ( $this->LastStartIp - $this->FirstStartIp ) / 7);
         if ($RecordCount <= 1){
             $this->Country = "FileDataError";
             fclose ( $this->fp );
             return 2;
         }
  
           $RangB= 0;
         $RangE= $RecordCount;
         // Match ...
         while ($RangB < $RangE-1)
         {
           $RecNo= floor(($RangB + $RangE) / 2);
           $this->getStartIp ( $RecNo );
  
             if ( $ip == $this->StartIp )
             {
                 $RangB = $RecNo;
                 break;
             }
           if ( $ip > $this->StartIp)
             $RangB= $RecNo;
           else
             $RangE= $RecNo;
         }
         $this->getStartIp ( $RangB );
         $this->getEndIp ( );
  
         if ( ( $this->StartIp   <= $ip ) && ( $this->EndIp >= $ip ) ){
             $nRet = 0;
             $this->getCountry ( );
             //這樣不太好..............所以..........
    $this->Local = str_replace("(我們一定要解放臺灣?。。。?, "", $this->Local);
    $this->Local = str_replace("CZ88.NET", "", $this->Local);
  
         }else {
             $nRet = 3;
             $this->Country = '未知';
             $this->Local = '';
    $this->Local = str_replace("CZ88.NET", "", $this->Local);
         }
         fclose ( $this->fp );
         return $nRet;
     }
}
  
  
function ip2location ( $ip )
{
     $wry = new TQQwry;
     $nRet = $wry->qqwry ( $ip );
     //可以利用 $nRet做一些事情,我是讓他自動記錄未知IP到一個表,代碼就不寫了。
     return $wry->Country.$wry->Local;
}
  
echo ip2location ("219.133.248.255");
?>

高效檢索 共享內存

<?php
/*
直接從共享內存中查找數據,而不需要再讀文件了,現在的查詢效率是原來的 1.5 倍。不過使用這個類要注意一點,這個類是一個 Singleton 類,所以需要用 & IpLocation::getInstance 來返回此類的實例引用,而不要用 new IpLocation 來創建實例,不然就不能保證實例的唯一性了。如果在一個頁面內創建多個 IpLocation 實例的話,會得到內存錯誤,嚴重情況下可能會使服務器崩潰,因此才把它定義為一個 Singleton 類。另外這個類的實例被創建一次后,文件內容就被讀入到共享內存中了,因此如果服務器不重新啟動,內存中的 QQWry.Dat 的文件數據就不會更新。
  
因為用了共享內存,因此對系統有一定的要求,如果系統是 Windows,系統需要 Windows2000 以上系統,PHP 作為 IIS 的 ISAPI 運行才支持共享內存,或者是 Linux 下 PHP 作為 Apache 模塊運行,CGI 和 CLI 方式下不可以。
  
*/
if( !function_exists('ftok') )
{
   function ftok($filename = "", $proj = "")
   {
       if( empty($filename) ││ !file_exists($filename) )
       {
           return -1;
       }
       else
       {
           $filename = $filename . (string) $proj;
           for($key = array(); sizeof($key) < strlen($filename); $key[] = ord(substr($filename, sizeof($key), 1)));
           return array_sum($key);
       }
   }
}
  
/**
* IP 地理位置查詢類
*
* @author 馬秉堯
* @version 2.5
* @copyright 2005 CoolCode.CN
*/
  
class IpLocation {
     /**
     * 共享內存編號
     *
     * @var int
     */
     var $shm_id;
     /**
     * 第一條IP記錄的偏移地址
     *
     * @var int
     */
     var $firstip;
  
     /**
     * 最后一條IP記錄的偏移地址
     *
     * @var int
     */
     var $lastip;
  
     /**
     * IP記錄的總條數(不包含版本信息記錄)
     *
     * @var int
     */
     var $totalip;
  
     /**
     * 當前共享內存位置指針
     *
     * @var int
     */
     var $pos;
  
     /**
     * 返回讀取的長整型數
     *
     * @access private
     * @return int
     */
     function getlong() {
         //將讀取的little-endian編碼的4個字節轉化為長整型數
         $result = unpack('Vlong', shmop_read($this->shm_id, $this->pos, 4));
         $this->pos += 4;
         return $result['long'];
     }
  
     /**
     * 返回讀取的3個字節的長整型數
     *
     * @access private
     * @return int
     */
     function getlong3() {
         //將讀取的little-endian編碼的3個字節轉化為長整型數
         $result = unpack('Vlong', shmop_read($this->shm_id, $this->pos, 3).chr(0));
         $this->pos += 3;
         return $result['long'];
     }
  
     /**
     * 返回壓縮后可進行比較的IP地址
     *
     * @access private
     * @param string $ip
     * @return string
     */
     function packip($ip) {
         // 將IP地址轉化為長整型數,如果在PHP5中,IP地址錯誤,則返回False,
         // 這時intval將Flase轉化為整數-1,之后壓縮成big-endian編碼的字符串
         return pack('N', intval(ip2long($ip)));
     }
  
     /**
     * 返回讀取的字符串
     *
     * @access private
     * @param string $data
     * @return string
     */
     function getstring($data = "") {
         $char = shmop_read($this->shm_id, $this->pos++, 1);
         while (ord($char) > 0) {         // 字符串按照C格式保存,以\0結束
             $data .= $char;             // 將讀取的字符連接到給定字符串之后
             $char = shmop_read($this->shm_id, $this->pos++, 1);
         }
         return $data;
     }
  
     /**
     * 返回地區信息
     *
     * @access private
     * @return string
     */
     function getarea() {
         $byte = shmop_read($this->shm_id, $this->pos++, 1); // 標志字節
         switch (ord($byte)) {
             case 0:                     // 沒有區域信息
                 $area = "";
                 break;
             case 1:
             case 2:                     // 標志字節為1或2,表示區域信息被重定向
                 $this->pos = $this->getlong3($this->pos);
                 $area = $this->getstring();
                 break;
             default:                     // 否則,表示區域信息沒有被重定向
                 $area = $this->getstring($byte);
                 break;
         }
         return $area;
     }
  
     /**
     * 根據所給 IP 地址或域名返回所在地區信息
     *
     * @access public
     * @param string $ip
     * @return array
     */
     function getlocation($ip) {
         if (!$this->shm_id) return null;     // 如果共享內存沒有被正確打開,則直接返回空
         $location['ip'] = gethostbyname($ip);   // 將輸入的域名轉化為IP地址
         $ip = $this->packip($location['ip']);   // 將輸入的IP地址轉化為可比較的IP地址
                                                 // 不合法的IP地址會被轉化為255.255.255.255
         // 對分搜索
         $l = 0;                             // 搜索的下邊界
         $u = $this->totalip;                 // 搜索的上邊界
         $findip = $this->lastip;             // 如果沒有找到就返回最后一條IP記錄(QQWry.Dat的版本信息)
         while ($l <= $u) {                   // 當上邊界小于下邊界時,查找失敗
             $i = floor(($l + $u) / 2);       // 計算近似中間記錄
             $this->pos = $this->firstip + $i * 7;
             $beginip = strrev(shmop_read($this->shm_id, $this->pos, 4));         // 獲取中間記錄的開始IP地址
             // strrev函數在這里的作用是將little-endian的壓縮IP地址轉化為big-endian的格式
             // 以便用于比較,后面相同。
             if ($ip < $beginip) {       // 用戶的IP小于中間記錄的開始IP地址時
                 $u = $i - 1;             // 將搜索的上邊界修改為中間記錄減一
             }
             else {
                 $this->pos += 4;
                 $this->pos = $this->getlong3();
                 $endip = strrev(shmop_read($this->shm_id, $this->pos, 4));   // 獲取中間記錄的結束IP地址
                 if ($ip > $endip) {     // 用戶的IP大于中間記錄的結束IP地址時
                     $l = $i + 1;         // 將搜索的下邊界修改為中間記錄加一
                 }
                 else {                   // 用戶的IP在中間記錄的IP范圍內時
                     $findip = $this->firstip + $i * 7;
                     break;               // 則表示找到結果,退出循環
                 }
             }
         }
  
         //獲取查找到的IP地理位置信息
         $this->pos = $findip;
         $location['beginip'] = long2ip($this->getlong());   // 用戶IP所在范圍的開始地址
         $this->pos = $offset = $this->getlong3();
         $location['endip'] = long2ip($this->getlong());     // 用戶IP所在范圍的結束地址
         $byte = shmop_read($this->shm_id, $this->pos++, 1); // 標志字節
         switch (ord($byte)) {
             case 1:                     // 標志字節為1,表示國家和區域信息都被同時重定向
                 $this->pos = $countryOffset = $this->getlong3();             // 重定向地址
                 $byte = shmop_read($this->shm_id, $this->pos++, 1);         // 標志字節
                 switch (ord($byte)) {
                     case 2:             // 標志字節為2,表示國家信息又被重定向
                         $this->pos = $this->getlong3();
                         $location['country'] = $this->getstring();
                         $this->pos = $countryOffset + 4;
                         $location['area'] = $this->getarea();
                         break;
                     default:             // 否則,表示國家信息沒有被重定向
                         $location['country'] = $this->getstring($byte);
                         $location['area'] = $this->getarea();
                         break;
                 }
                 break;
             case 2:                     // 標志字節為2,表示國家信息被重定向
                 $this->pos = $this->getlong3();
                 $location['country'] = $this->getstring();
                 $this->pos = $offset + 8;
                 $location['area'] = $this->getarea();
                 break;
             default:                     // 否則,表示國家信息沒有被重定向
                 $location['country'] = $this->getstring($byte);
                 $location['area'] = $this->getarea();
                 break;
         }
         if ($location['country'] == " CZ88.NET") {   //   CZ88.NET表示沒有有效信息
             $location['country'] = "未知";
         }
         if ($location['area'] == " CZ88.NET") {
             $location['area'] = "";
         }
         return $location;
     }
  
     /**
     * 構造函數
     *
     * @param string $filename
     * @return IpLocation
     */
     function IpLocation($filename) {
         $shm_key = ftok($filename, 'R');
         if (!($this->shm_id = @shmop_open($shm_key, "a", 0, 0))) {   // 如果沒有建立共享內存塊
             $content = file_get_contents($filename);                 // 則讀取文件內容
             $this->shm_id = shmop_open($shm_key, "c", 0644, strlen($content));
             shmop_write($this->shm_id, $content, 0);                 // 并將其寫入新建的共享內存塊
         }
         $this->pos = 0;
         $this->firstip = $this->getlong();
         $this->lastip = $this->getlong();
         $this->totalip = ($this->lastip - $this->firstip) / 7;
         //注冊析構函數,使其在程序執行結束時執行
         register_shutdown_function(array(&$this, '_IpLocation'));
     }
  
     /**
     * 析構函數,用于在頁面執行結束后自動關閉打開的文件。
     *
     */
     function _IpLocation() {
         shmop_close($this->shm_id);
     }
  
     /**
     * 本類為一個 Singleton 類,必須用下面的函數來返回實例
     *
     * @param string $filename
     * @return &IpLocation
     */
     function &getInstance($filename = "QQWry.Dat") {
         static $instance = null;
         if (is_null($instance)) {
             $instance = new IpLocation($filename);
         }
         return $instance;
     }
}
?>

該文章在 2012/8/8 15:02:36 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 国产午夜精品一区二区三区四 | 国产精品免费观看一区二区 | 波多野结衣人妻无码潮喷av | 国产一区二区三区韩国女主播 | 成年女人a毛片免费视频 | 高潮又黄又爽又无遮挡又免费视频 | 国内精品一卡2卡3卡4卡三卡 | 国产成人片 | 2025国产拍视频最好的手机 | 2025国产精品系列一区二区 | 91精品成人影院 | 国产精品一区二区三区剧情片 | 国产精品美女视频免费观看 | 精品无码一区二区三区视在线 | 国产精品一区二区久久精品不卡 | 成人一区二区三区漫画 | 国产成人高清在线观看视频 | 国产成人精欧美精品视频 | 丰满熟妇人妻中文 | h无码精品视频在线观看网站 | 国产成人亚洲精品电影香蕉 | 国产蜜桃tv一区二区无码 | 精品无人国产偷自产在线日本 | 国产日韩aⅴ无码一区二区三区 | 国产高潮流白浆喷水免费视频 | 国产精品一区二区av麻豆 | 国产成人综合亚洲欧美 | 国产精品疯狂输出白丝jk | 国产精品毛片一区二区三区在线 | 国产精品天干天干在线观看 | 国产精品毛片久久久久久久 | 极品少妇一区二区三区四区 | 国产无套推油按摩女视频推油 | 国产毛片久久精品 | 国产午夜片无码区在线观看爱情 | 国产精品毛片a∨一区二区三区 | 国产女同女互慰 | 国产成人精品日本亚洲18图 | 国产91在线播 | 国产午夜精品亚洲精品国产 | 国产猛烈高潮尖叫视频免费 |