PHP read Pure IP Database usage Example

Source: Internet
Author: User
Tags fread ord unpack

  This article mainly introduces the PHP read the pure IP database display details of the use of examples, we refer to the use of the bar

  Code as follows: <?php/*-------------------------------------------------- ip2address [Qqwry.dat]------------ --------------------------------------*/  class IP { var $fh;//ip database file handle  var $first;//First Index  var $last; The last index  var $total; Total indexes    //Constructors  function __construct () {  $this->fh = fopen (' Qqwry.dat ', ' RB ');//qqwry.dat file &n Bsp $this->first = $this->getlong4 ();   $this->last = $this->getlong4 ();   $this->total = ($this->last-$this->first)/7; 7 bytes per index &nbsp    //check IP legality  function checkip ($ip) {  $arr = explode ('. ', $ip);   if ($ar R)!=4 {   return false;  } else {   for ($i =0; $i < 4; $i + +) {    if ($arr [$i ] < ' 0 ' | | $arr [$i] > ' 255 ') {     return false;    }      }   return true; &NBSP}    function GetLong4 () { /Read Little-endian encoded 4 bytes ConversionFor long integer   $result = unpack (' Vlong ', fread ($this->fh, 4));   return $result [' Long ']; &NBSP}    function GetLong3 () { /Read Little-endian encoded 3 bytes converted to Long integer   $result = unpack (' Vlong ', fread ( $this->FH, 3). chr (0));   return $result [' Long ']; &NBSP}    //Query information  function getInfo ($data = "") {  $char = fread ($this->fh, 1);   while (Ord ($char)!= 0) {//Country area information ends with 0     $data. = $char     $char = fread ($this->fh, 1); &N Bsp }   return $data; &NBSP}    //query area information  function Getarea () {  $byte = fread ($this->fh, 1);//flag byte   switch (ORD ( $byte)) {   case 0: $area = '; break;//No area information    case 1://Region redirected     fseek ($this->fh, $this->getlong3 ());     $area = $this->getinfo (); Break    case 2://Area redirected    fseek ($this->fh, $this->getlong3 ());     $area = $this->getinfo (); Break    default: $area = $this->getinfo ($byte);  break; The area has not been redirected  }   return $area; &NBSP}    function ip2addr ($IP) {  if (! $this-> Checkip ($ip)) {   return false;  } &NB Sp   $IP = Pack (' N ', Intval (Ip2long ($IP));    //Two point search   $l = 0;   $r = $this->total;     while ($l <= $r) {    $m = Floor (($l + $r)/2)//COMPUTE Intermediate index    fseek ($this->fh, $this ->first + $m * 7);     $beginip = Strrev (fread ($this->fh, 4)); The start IP address of the intermediate index    fseek ($this->fh, $this->getlong3 ());     $ENDIP = Strrev (fread ($this->fh, 4)); End IP address of intermediate index      if ($ip < $beginip) {//user's IP is less than the start IP address of intermediate index     $r = $m-1;    } If {    if ($ip > $endip) {//user's IP is greater than the end IP address of the intermediate index       $l = $m + 1;    } else { User IP within the IP range of the intermediate index       $FINDIP = $this->first + $m * 7;      break; &nBsp  }    }      //Query national area Information   fseek ($this->fh, $findip);   $location [' beginip '] = Long2ip ($this->getlong4 ()); Start address   $offset = $this->getlong3 () for User IP range;   Fseek ($this->fh, $offset);   $location [' endip '] = Long2ip ($this->getlong4 ()); End address of User IP range   $byte = fread ($this->fh, 1); Flag bytes   Switch (ord ($byte)) {   case 1:  //National and regional information is redirected     $countryOffset = $this->get Long3 (); REDIRECT Address     Fseek ($this->fh, $countryOffset);     $byte = fread ($this->fh, 1); Flag bytes     switch (ORD ($byte)) {     case 2://National information was two times redirected       fseek ($this->f H, $this->getlong3 ());       $location [' country '] = $this->getinfo ();       fseek ($this->fh, $countryOffset + 4);       $location [' area '] = $this->getarea ();       break;      default://CountryHome information has not been two times redirected       $location [' country '] = $this->getinfo ($byte);       $location [' area '] = $this->getarea ();       break;    }     break;      case 2://Country information redirected     fseek ($this->fh, $this->getlong3 ());     $location [' country '] = $this->getinfo ();     Fseek ($this->fh, $offset + 8);     $location [' area '] = $this->getarea ();     break;      default://Country information has not been redirected     $location [' country '] = $this->getinfo ($byte);     $location [' area '] = $this->getarea ();     break;  }    //gb2312 to Utf-8 (cz88.net when removing no information)   foreach ($location as $k => $v) {    $loca tion[$k] = Str_replace (' CZ88. NET ', ', Iconv (' gb2312 ', ' utf-8 ', $v));  }     return $location; &NBSP}    //destructor  function __destruct () {  fclose ($this->fh);  }} &nbsP $ip = new IP (); $addr = $ip-> ip2addr (' IP address '); Print_r ($ADDR);?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.