This article describes how to use php to read detailed information from a pure ip database.
This article describes how to use php to read detailed information from a pure ip database.
The Code is as follows:
/*--------------------------------------------------
Ip2address [qqwry. dat]
--------------------------------------------------*/
Class ip {
Var $ fh; // IP address database file handle
Var $ first; // The first index
Var $ last; // The last index
Var $ total; // The total number of indexes.
// 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; // each index contains 7 bytes.
}
// Check IP Validity
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]> '123 '){
Return false;
}
}
}
Return true;
}
Function getLong4 (){
// Read the four bytes of little-endian encoding and convert them into long integer values.
$ Result = unpack ('vlong', fread ($ this-> fh, 4 ));
Return $ result ['long'];
}
Function getLong3 (){
// Read the three bytes of little-endian encoding and convert them into long integer values.
$ Result = unpack ('vlong', fread ($ this-> fh, 3). chr (0 ));
Return $ result ['long'];
}
// Query information
Function getInfo ($ data = ""){
$ Char = fread ($ this-> fh, 1 );
While (ord ($ char )! = 0) {// the country and region information ends with 0
$ Data. = $ char;
$ Char = fread ($ this-> fh, 1 );
}
Return $ data;
}
// Query region information
Function getArea (){
$ Byte = fread ($ this-> fh, 1); // flag byte
Switch (ord ($ byte )){
Case 0: $ area = ''; break; // No region information
Case 1: // The region is redirected.
Fseek ($ this-> fh, $ this-> getLong3 ());
$ Area = $ this-> getInfo (); break;
Case 2: // The region is redirected.
Fseek ($ this-> fh, $ this-> getLong3 ());
$ Area = $ this-> getInfo (); break;
Default: $ area = $ this-> getInfo ($ byte); break; // The region is not redirected.
}
Return $ area;
}
Function 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); // calculates the intermediate Index
Fseek ($ this-> fh, $ this-> first + $ m * 7 );
$ Beginip = strrev (fread ($ this-> fh, 4); // start IP address of the intermediate Index
Fseek ($ this-> fh, $ this-> getLong3 ());
$ Endip = strrev (fread ($ this-> fh, 4); // end IP address of the intermediate Index
If ($ ip <$ beginip) {// when the user's IP address is smaller than the starting ip address of the intermediate Index
$ R = $ m-1;
} Else {
If ($ ip> $ endip) {// when the user's IP address is greater than the end ip address of the intermediate Index
$ L = $ m + 1;
} Else {// when the user IP address is within the IP address range of the intermediate Index
$ Findip = $ this-> first + $ m * 7;
Break;
}
}
}
// Query country and region information
Fseek ($ this-> fh, $ findip );
$ Location ['ininip'] = long2ip ($ this-> getLong4 (); // start address of the user IP address range
$ Offset = $ this-> getlong3 ();
Fseek ($ this-> fh, $ offset );
$ Location ['enabled'] = long2ip ($ this-> getLong4 (); // end address of the user's IP address range
$ Byte = fread ($ this-> fh, 1); // flag byte
Switch (ord ($ byte )){
Case 1: // country and region 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 is redirected twice
Fseek ($ this-> fh, $ this-> getLong3 ());
$ Location ['country'] = $ this-> getInfo ();
Fseek ($ this-> fh, $ countryOffset + 4 );
$ Location ['region'] = $ this-> getArea ();
Break;
Default: // country information is not redirected twice
$ Location ['country'] = $ this-> getInfo ($ byte );
$ Location ['region'] = $ this-> getArea ();
Break;
}
Break;
Case 2: // country information is redirected
Fseek ($ this-> fh, $ this-> getLong3 ());
$ Location ['country'] = $ this-> getInfo ();
Fseek ($ this-> fh, $ offset + 8 );
$ Location ['region'] = $ this-> getArea ();
Break;
Default: // country information not redirected
$ Location ['country'] = $ this-> getInfo ($ byte );
$ Location ['region'] = $ this-> getArea ();
Break;
}
// Gb2312 to UTF-8 (display CZ88.NET without information)
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 ');
Print_r ($ addr );
?>
,