您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)利用php怎么對(duì)qqwry.dat的 ip地址進(jìn)行讀取,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
實(shí)例如下:
<?php // +---------------------------------------------------------------------- // | // +---------------------------------------------------------------------- // | // +---------------------------------------------------------------------- class iplocate{ var $fp; var $firstip; //第一條ip索引的偏移地址 var $lastip; //最后一條ip索引的偏移地址 var $totalip; //總ip數(shù) // 獲取客戶端IP地址 function get_client_ip(){ if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) $ip = getenv("HTTP_CLIENT_IP"); else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) $ip = getenv("HTTP_X_FORWARDED_FOR"); else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) $ip = getenv("REMOTE_ADDR"); else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) $ip = $_SERVER['REMOTE_ADDR']; else $ip = "unknown"; return($ip); } //* //構(gòu)造函數(shù),初始化一些變量 //$datfile 的值為純真IP數(shù)據(jù)庫的名子,可自行修改. //* function iplocate(){ $datfile = "ip1.dat"; $this->fp=fopen($datfile,'rb'); //二制方式打開 $this->firstip = $this->get4b(); //第一條ip索引的絕對(duì)偏移地址 $this->lastip = $this->get4b(); //最后一條ip索引的絕對(duì)偏移地址 $this->totalip =($this->lastip - $this->firstip)/7 ; //ip總數(shù) 索引區(qū)是定長(zhǎng)的7個(gè)字節(jié),在此要除以7, register_shutdown_function(array($this,"closefp")); //為了兼容php5以下版本,本類沒有用析構(gòu)函數(shù),自動(dòng)關(guān)閉ip庫. } //* //關(guān)閉ip庫 //* function closefp(){ fclose($this->fp); } //* //讀取4個(gè)字節(jié)并將解壓成long的長(zhǎng)模式 //* function get4b(){ $str=@unpack("V",fread($this->fp,4)); return $str[1]; } //* //讀取重定向了的偏移地址 //* function getoffset(){ $str=@unpack("V",fread($this->fp,3).chr(0)); return $str[1]; } //* //讀取ip的詳細(xì)地址信息 //* function getstr(){ $split=fread($this->fp,1); while (ord($split)!=0) { $str .=$split; $split=fread($this->fp,1); } return $str; } //* //將ip通過ip2long轉(zhuǎn)成ipv4的互聯(lián)網(wǎng)地址,再將他壓縮成big-endian字節(jié)序 //用來和索引區(qū)內(nèi)的ip地址做比較 //* function iptoint($ip){ return pack("N",intval(ip2long($ip))); } //* //獲取客戶端ip地址 //注意:如果你想要把ip記錄到服務(wù)器上,請(qǐng)?jiān)趯憥鞎r(shí)先檢查一下ip的數(shù)據(jù)是否安全. //* function getIP() { if (getenv('HTTP_CLIENT_IP')) { $ip = getenv('HTTP_CLIENT_IP'); } elseif (getenv('HTTP_X_FORWARDED_FOR')) { //獲取客戶端用代理服務(wù)器訪問時(shí)的真實(shí)ip 地址 $ip = getenv('HTTP_X_FORWARDED_FOR'); } elseif (getenv('HTTP_X_FORWARDED')) { $ip = getenv('HTTP_X_FORWARDED'); } elseif (getenv('HTTP_FORWARDED_FOR')) { $ip = getenv('HTTP_FORWARDED_FOR'); } elseif (getenv('HTTP_FORWARDED')) { $ip = getenv('HTTP_FORWARDED'); } else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip; } //* //獲取地址信息 //* function readaddress(){ $now_offset=ftell($this->fp); //得到當(dāng)前的指針位址 $flag=$this->getflag(); switch (ord($flag)){ case 0: $address=""; break; case 1: case 2: fseek($this->fp,$this->getoffset()); $address=$this->getstr(); break; default: fseek($this->fp,$now_offset); $address=$this->getstr(); break; } return $address; } //* //獲取標(biāo)志1或2 //用來確定地址是否重定向了. //* function getflag(){ return fread($this->fp,1); } //* //用二分查找法在索引區(qū)內(nèi)搜索ip //* function searchip($ip){ $ip=gethostbyname($ip); //將域名轉(zhuǎn)成ip $ip_offset["ip"]=$ip; $ip=$this->iptoint($ip); //將ip轉(zhuǎn)換成長(zhǎng)整型 $firstip=0; //搜索的上邊界 $lastip=$this->totalip; //搜索的下邊界 $ipoffset=$this->lastip; //初始化為最后一條ip地址的偏移地址 while ($firstip <= $lastip){ $i=floor(($firstip + $lastip) / 2); //計(jì)算近似中間記錄 floor函數(shù)記算給定浮點(diǎn)數(shù)小的最大整數(shù),說白了就是四舍五也舍 fseek($this->fp,$this->firstip + $i * 7); //定位指針到中間記錄 $startip=strrev(fread($this->fp,4)); //讀取當(dāng)前索引區(qū)內(nèi)的開始ip地址,并將其little-endian的字節(jié)序轉(zhuǎn)換成big-endian的字節(jié)序 if ($ip < $startip) { $lastip=$i - 1; } else { fseek($this->fp,$this->getoffset()); $endip=strrev(fread($this->fp,4)); if ($ip > $endip){ $firstip=$i + 1; } else { $ip_offset["offset"]=$this->firstip + $i * 7; break; } } } return $ip_offset; } //* //獲取ip地址詳細(xì)信息 //* function getaddress($ip){ $ip_offset=$this->searchip($ip); //獲取ip 在索引區(qū)內(nèi)的絕對(duì)編移地址 $ipoffset=$ip_offset["offset"]; $address["ip"]=$ip_offset["ip"]; fseek($this->fp,$ipoffset); //定位到索引區(qū) $address["startip"]=long2ip($this->get4b()); //索引區(qū)內(nèi)的開始ip 地址 $address_offset=$this->getoffset(); //獲取索引區(qū)內(nèi)ip在ip記錄區(qū)內(nèi)的偏移地址 fseek($this->fp,$address_offset); //定位到記錄區(qū)內(nèi) $address["endip"]=long2ip($this->get4b()); //記錄區(qū)內(nèi)的結(jié)束ip 地址 $flag=$this->getflag(); //讀取標(biāo)志字節(jié) switch (ord($flag)) { case 1: //地區(qū)1地區(qū)2都重定向 $address_offset=$this->getoffset(); //讀取重定向地址 fseek($this->fp,$address_offset); //定位指針到重定向的地址 $flag=$this->getflag(); //讀取標(biāo)志字節(jié) switch (ord($flag)) { case 2: //地區(qū)1又一次重定向, fseek($this->fp,$this->getoffset()); $address["area1"]=$this->getstr(); fseek($this->fp,$address_offset+4); //跳4個(gè)字節(jié) $address["area2"]=$this->readaddress(); //地區(qū)2有可能重定向,有可能沒有 break; default: //地區(qū)1,地區(qū)2都沒有重定向 fseek($this->fp,$address_offset); //定位指針到重定向的地址 $address["area1"]=$this->getstr(); $address["area2"]=$this->readaddress(); break; } break; case 2: //地區(qū)1重定向 地區(qū)2沒有重定向 $address1_offset=$this->getoffset(); //讀取重定向地址 fseek($this->fp,$address1_offset); $address["area1"]=$this->getstr(); fseek($this->fp,$address_offset+8); $address["area2"]=$this->readaddress(); break; default: //地區(qū)1地區(qū)2都沒有重定向 fseek($this->fp,$address_offset+4); $address["area1"]=$this->getstr(); $address["area2"]=$this->readaddress(); break; } //*過濾一些無用數(shù)據(jù) if (strpos($address["area1"],"CZ88.NET")!=false){ $address["area1"]="未知"; } if (strpos($address["area2"],"CZ88.NET")!=false){ $address["area2"]=" "; } foreach($address as $k=>$item) { if(!$this->is_utf8($address[$k])) { $address[$k] = iconv('gbk','utf-8',$item); } } return $address; } function is_utf8($string) { return preg_match('%^(?: [\x09\x0A\x0D\x20-\x7E] # ASCII | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 )*$%xs', $string); } } ?>
以上就是利用php怎么對(duì)qqwry.dat的 ip地址進(jìn)行讀取,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。