<pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

          <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

          <p id="bbfd9"></p>

          <p id="bbfd9"><cite id="bbfd9"></cite></p>

            <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

            <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
            <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>

            如何進行PHP查詢ip所在地

            時間:2025-09-22 02:11:13 php語言

            如何進行PHP查詢ip所在地

              導語:如何進行PHP查詢ip所在地呢?下面是小編給大家提供的實現方法,大家可以參考閱讀,更多詳情請關注應屆畢業生考試網。

              具體實現方法如下:

              代碼如下:

              <?php

              /**

              *@ date         2010.12.21

              注:文件頭 [第一條索引的偏移量 (4byte)] + [最后一條索引的偏移地址 (4byte)]     8字節

              記錄區 [結束ip (4byte)] + [地區1] + [地區2]                                4字節+不定長

              索引區 [開始ip (4byte)] + [指向記錄區的偏移地址 (3byte)]                   7字節

              */

              class iplocation{

              var $fp;

              var $firstip;  /pic/p>

              var $lastip;   /pic/p>

              var $totalip;  /pic/p>

              /*

              |----------------------------------------------------------------------------

              | 構造函數,初始化一些變量

              |----------------------------------------------------------------------------

              |

              */

              function iplocation($datfile = "qqwry.dat"){

              $this->fp=fopen($datfile,'rb')or die("qqwry.dat不存在,請去網上 <a href='/pic/softs/10529.html'>下載純真ip數據 庫</a>, 'qqwry.dat' 放到當前目錄下");   /pic/p>

              $this->firstip = $this->get4b(); /pic/p>

              $this->lastip = $this->get4b();  /pic/p>

              $this->totalip =($this->lastip - $this->firstip)/7 ; /pic/p>

              register_shutdown_function(array($this,"closefp"));  /pic/p>

              }

              /*

              |----------------------------------------------------------------------------

              | 關閉ip庫

              |----------------------------------------------------------------------------

              |

              */

              function closefp(){

              fclose($this->fp);

              }

              /*

              |----------------------------------------------------------------------------

              | 讀取4個字節并將解壓成long的長模式

              |----------------------------------------------------------------------------

              |

              */

              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的詳細地址信息

              |----------------------------------------------------------------------------

              |

              */

              function getstr(){

              $split=fread($this->fp,1);

              while (ord($split)!=0) {

              $str .=$split;

              $split=fread($this->fp,1);

              }

              return $str;

              }

              /*

              |----------------------------------------------------------------------------

              | 將ip通過ip2long轉成ipv4的互聯網地址,再將他壓縮成big-endian字節序 ,用來和索引區內的ip地址做比較

              |----------------------------------------------------------------------------

              |

              */

              function iptoint($ip){

              return pack("n",intval(ip2long($ip)));

              }

              /*

              |----------------------------------------------------------------------------

              | 獲取地址信息

              |----------------------------------------------------------------------------

              |

              */

              function readaddress(){

              $now_offset=ftell($this->fp); /pic/p>

              $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;

              }

              /*

              |----------------------------------------------------------------------------

              | 獲取標志1或2   用來確定地址是否重定向了

              |----------------------------------------------------------------------------

              |

              */

              function getflag(){

              return fread($this->fp,1);

              }

              /*

              |----------------------------------------------------------------------------

              | 用二分查找法在索引區內搜索ip

              |----------------------------------------------------------------------------

              |

              */

              function searchip($ip){

              $ip=gethostbyname($ip);     /pic/p>

              $ip_offset["ip"]=$ip;

              $ip=$this->iptoint($ip);    /pic/p>

              $firstip=0;                 /pic/p>

              $lastip=$this->totalip;     /pic/p>

              $ipoffset=$this->lastip;    /pic/p>

              while ($firstip <= $lastip){

              $i=floor(($firstip + $lastip) / 2);          /pic/p>

              fseek($this->fp,$this->firstip + $i * 7);    /pic/p>

              $startip=strrev(fread($this->fp,4));         /pic/p>

              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地址詳細信息

              |----------------------------------------------------------------------------

              |

              */

              function getaddress($ip){

              $ip_offset=$this->searchip($ip);  /pic/p>

              $ipoffset=$ip_offset["offset"];

              $address["ip"]=$ip_offset["ip"];

              fseek($this->fp,$ipoffset);      /pic/p>

              $address["startip"]=long2ip($this->get4b()); /pic/p>

              $address_offset=$this->getoffset();            /pic/p>

              fseek($this->fp,$address_offset);            /pic/p>

              $address["endip"]=long2ip($this->get4b());   /pic/p>

              $flag=$this->getflag();                      /pic/p>

              switch (ord($flag)) {

              case 1:  /pic/p>

              $address_offset=$this->getoffset();   /pic/p>

              fseek($this->fp,$address_offset);     /pic/p>

              $flag=$this->getflag();               /pic/p>

              switch (ord($flag)) {

              case 2:  /pic/p>

              fseek($this->fp,$this->getoffset());

              $address["area1"]=$this->getstr();

              fseek($this->fp,$address_offset+4);      /pic/p>

              $address["area2"]=$this->readaddress();  /pic/p>

              break;

              default: /pic/p>

              fseek($this->fp,$address_offset);        /pic/p>

              $address["area1"]=$this->getstr();

              $address["area2"]=$this->readaddress();

              break;

              }

              break;

              case 2: /pic/p>

              $address1_offset=$this->getoffset();   /pic/p>

              fseek($this->fp,$address1_offset);

              $address["area1"]=$this->getstr();

              fseek($this->fp,$address_offset+8);

              $address["area2"]=$this->readaddress();

              break;

              default: /pic/p>

              fseek($this->fp,$address_offset+4);

              $address["area1"]=$this->getstr();

              $address["area2"]=$this->readaddress();

              break;

              }

              /pic/p>

              if (strpos($address["area1"],"cz88.net")!=false){

              $address["area1"]="未知";

              }

              if (strpos($address["area2"],"cz88.net")!=false){

              $address["area2"]=" ";

              }

              return $address;

              }

              }

              /*用法如下:*/

              $ip=new iplocation("qqwry.dat");

              $address=$ip->getaddress("61.129.51.27");

              /pic/p>

              echo '<pre>';

              print_r($address);

              ?>

            【如何進行PHP查詢ip所在地】相關文章:

            php如何獲得ip地址10-30

            怎么查詢電腦ip地址09-14

            PHP對特殊語文查詢結果進行數組排序的方法介紹03-07

            php對特殊語句查詢結果進行數組排序的方法技巧02-06

            php查詢mysql的實例07-27

            PHP與MYSql連接與查詢11-30

            PHP獲取真實的客戶IP的方法01-27

            實現PHP實現限制IP訪問02-23

            局域網中如何快速查詢有效IP地址03-17

                    <pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

                    <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

                    <p id="bbfd9"></p>

                    <p id="bbfd9"><cite id="bbfd9"></cite></p>

                      <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

                      <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
                      <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
                      飘沙影院