PHP read Pure IP address database

Source: Internet
Author: User
Tags fread ord unpack

<?php/*--------------------------------------------------Ip2address [qqwry.dat]----------------------- ---------------------------*/class ip {var  $fh;  //ip database file handle var  $first;  // The first index var  $last;  //the last index var  $total;  //Index Total//constructor function __construct ()  {$this- >fh = fopen (' Qqwry.dat ',  ' RB ');  //qqwry.dat file $this->first =  $this GetLong4 (); $this->last =  $this->getlong4 (); $this->total =  ($this->last -   $this->first)  / 7; //per index 7 bytes}//Check IP legitimacy function checkip ($IP)  {$arr  =  explode ('. ', $ip); if (count ($arr)  !=4 )  {return false;}  else {for  ($i =0;  $i  < 4;  $i + +)  {if  ($arr [$i] < ' 0 '  | |   $arr [$i] >  ' 255 ')  {return false;}} Return true;} Function getlong4 ()  {//reads Little-endian encoded 4 bytes into a long integer $result = uNpack (' Vlong ',  fread ($this->fh, 4));return  $result [' Long '];} Function getlong3 ()  {//reads Little-endian encoded 3 bytes into a long integer $result = unpack (' Vlong ',  fread ($ this->fh, 3). chr (0));return  $result [' Long '];} Query Information Function getinfo ($data  =  ")  {$char  = fread ($this->fh, 1);   (Ord ($char)  != 0)  { //National area information with 0 end of $data .=  $char; $char  = fread ($ THIS-&GT;FH,&NBSP;1);} return  $data;} Query Area Information Function getarea ()  {$byte  = fread ($this->fh, 1);  //Flag byte switch  ( Ord ($byte))  {case 0:  $area  =  ";  break; //No regional information case 1: // Area 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); &nbThe sp; break; //region was not redirected to the}return  $area;} FUNCTION&NBSP;IP2ADDR ($IP)  {if (! $this  -> checkip ($ip)) {return false;} $ip  = pack (' N ',  intval (Ip2long ($IP)));//binary 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,&NBSP;4)  //The start IP address of the intermediate index fseek ($this->fh,  $this->getlong3 ()); $endip  = strrev (fread ($this->fh, 4));  //the end IP address of the intermediate index if  ($ip  <  $beginip)  { //when the user's IP is less than the start IP address of the intermediate index $r =  $m  - 1;}  else {if  ($ip  >  $endip)  { //user's IP is greater than the end IP address of the intermediate index $l =  $m  +  1;}  else { //User IP is within the IP range of the intermediate index $findip =  $this->first +  $m  * 7; Break;}}} Find out more about the national area Information fseek ($this->fh,  $finDIP), $location [' Beginip '] = long2ip ($this->getlong4 ());  //the start address of the range where the user IP is located $offset = $ This->getlong3 (); Fseek ($this->fh,  $offset); $location [' EndIP '] = long2ip ($this GetLong4 ())  //the end address $byte = fread ($this->fh, 1) of the range where the user IP is located,  //flag byte switch  (ORD ($ BYTE)  {case 1:  //national and regional information is redirected $countryoffset =  $this->getlong3 ();  // REDIRECT Address fseek ($this->fh,  $countryOffset); $byte  = fread ($this->fh, 1);  //Flag byte switch   (ORD ($byte))  {case 2: //Country information was redirected two times fseek ($this->fh,  $this->getlong3 ()); location[' Country '] =  $this->getinfo (); Fseek ($this->fh,  $countryOffset  + 4); $ location[' area '] =  $this->getarea (); break;default: //Country Info was not redirected two times $location[' country '] =   $this->getinfo ($byte) $location [' area '] =  $this->getarea (); break;case 2: //Country Info redirected fseek ($this->fh,  $this->getlong3 ()); $location [' Country '] =  $this->getinfo (); Fseek ($this->fh, $ OFFSET&NBSP;+&NBSP;8); $location [' Area '] =  $this->getarea (); break;default: //Country info not redirected $ location[' Country '] =  $this->getinfo ($byte); $location [' Area '] =  $this->getarea (); break;} Gb2312 to utf-8 (Cz88.net displayed when no information is removed) foreach  ($location  as  $k  =>  $v)  {$location [$k] = str_replace (' cz88.net ', ', ' Iconv (' gb2312 ',  ' utf-8 ',  $v));} return  $location;} destructor function __destruct ()  {fclose ($this->fh);}} $ip  = new ip (); $addr  =  $ip  -> ip2addr (' IP address ');p Rint_r ($ADDR); >

PHP reads the pure IP address database

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.