Php: How to query ip address location _ PHP Tutorial

Source: Internet
Author: User
Tags floor function index php
Php queries the ip address location. The php method for querying the ip address location is as follows :? Php *** @ date2010.12.21 Note: [offset of the first index (4 byte)] + [offset of the last index php method for querying the ip address location

The specific implementation method is as follows:

The code is as follows:

/**
* @ Date 2010.12.21
Note: the file header [offset of the first index (4 byte)] + [offset address of the last index (4 byte)] 8 bytes
Record area [end ip (4 byte)] + [Region 1] + [Region 2] 4 bytes + indefinite length
Index area [start ip (4 byte)] + [point to the record area offset address (3 byte)] 7 bytes
*/
Class iplocation {
Var $ fp;
Var $ firstip; // The offset address of the first ip index
Var $ lastip; // the offset of the last ip index
Var $ totalip; // total number of ip addresses
/*
| ----------------------------------------------------------------------------
| Constructor, initializing some variables
| ----------------------------------------------------------------------------
|
*/
Function iplocation ($ datfile = "qqwry. dat "){
$ This-> fp = fopen ($ datfile, 'RB') or die ("qqwry. dat does not exist. please download the pure ip address data library, 'qqwry. dat 'put in the current directory "); // enable it in binary mode
$ This-> firstip = $ this-> get4b (); // The absolute offset address of the first ip index
$ This-> lastip = $ this-> get4b (); // The absolute offset address of the last ip index
$ This-> totalip = ($ this-> lastip-$ this-> firstip)/7; // The total number of ip addresses in the index area is a fixed length of 7 bytes, divide it by 7,
Register_shutdown_function (array ($ this, "closefp"); // to be compatible with versions earlier than php5, this class automatically closes the ip library without destructor.
}
/*
| ----------------------------------------------------------------------------
| Disable the ip address Library
| ----------------------------------------------------------------------------
|
*/
Function closefp (){
Fclose ($ this-> fp );
}
/*
| ----------------------------------------------------------------------------
| Read 4 bytes and decompress them to the long mode
| ----------------------------------------------------------------------------
|
*/
Function get4b (){
$ Str = unpack ("v", fread ($ this-> fp, 4 ));
Return $ str [1];
}
/*
| ----------------------------------------------------------------------------
| Read the redirected offset address
| ----------------------------------------------------------------------------
|
*/
Function getoffset (){
$ Str = unpack ("v", fread ($ this-> fp, 3). chr (0 ));
Return $ str [1];
}
/*
| ----------------------------------------------------------------------------
| Read detailed ip address information
| ----------------------------------------------------------------------------
|
*/
Function getstr (){
$ Split = fread ($ this-> fp, 1 );
While (ord ($ split )! = 0 ){
$ Str. = $ split;
$ Split = fread ($ this-> fp, 1 );
}
Return $ str;
}
/*
| ----------------------------------------------------------------------------
| Convert an ip address through ip2long to an ipv4 Internet address, compress it into the big-endian byte order, and use it to compare the ip address in the index area
| ----------------------------------------------------------------------------
|
*/
Function iptoint ($ ip ){
Return pack ("n", intval (ip2long ($ ip )));
}
/*
| ----------------------------------------------------------------------------
| Get address information
| ----------------------------------------------------------------------------
|
*/
Function readaddress (){
$ Now_offset = ftell ($ this-> fp); // Obtain the current pointer address.
$ 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;
}
/*
| ----------------------------------------------------------------------------
| The Get flag 1 or 2 is used to determine whether the address is redirected.
| ----------------------------------------------------------------------------
|
*/
Function getflag (){
Return fread ($ this-> fp, 1 );
}
/*
| ----------------------------------------------------------------------------
| Search for ip addresses in the index area using the binary search method
| ----------------------------------------------------------------------------
|
*/
Function searchip ($ ip ){
$ Ip = gethostbyname ($ ip); // Convert the domain name to an ip address
$ Ip_offset ["ip"] = $ ip;
$ Ip = $ this-> iptoint ($ ip); // Convert the ip address into a growth integer.
$ Firstip = 0; // search upper boundary
$ Lastip = $ this-> totalip; // The bottom boundary of the search.
$ Ipoffset = $ this-> lastip; // The offset address initialized to the last IP address.
While ($ firstip <= $ lastip ){
$ I = floor ($ firstip + $ lastip)/2); // calculate the approximate intermediate record floor function to calculate the maximum integer of a given floating point number, to put it bluntly, four homes and five homes are also available.
Fseek ($ this-> fp, $ this-> firstip + $ I * 7); // locate the pointer to the intermediate record
$ Startip = strrev (fread ($ this-> fp, 4); // read the starting IP address in the current index area, and converts the byte order of its little-endian to the byte order of big-endian.
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;
}
/*
| ----------------------------------------------------------------------------
| Obtain IP address details
| ----------------------------------------------------------------------------
|
*/
Function getaddress ($ ip ){
$ Ip_offset = $ this-> searchip ($ ip); // Obtain the absolute address of the ip in the index area.
$ Ipoffset = $ ip_offset ["offset"];
$ Address ["ip"] = $ ip_offset ["ip"];
Fseek ($ this-> fp, $ ipoffset); // locate the index area
$ Address ["startip"] = long2ip ($ this-> get4b (); // start ip address in the index area
$ Address_offset = $ this-> getoffset (); // obtains the offset of the ip address in the ip record area in the index area.
Fseek ($ this-> fp, $ address_offset); // locate to the record area
$ Address ["endip"] = long2ip ($ this-> get4b (); // end ip address in the record area
$ Flag = $ this-> getflag (); // read the flag byte
Switch (ord ($ flag )){
Case 1: // region 1 region 2 are Redirected
$ Address_offset = $ this-> getoffset (); // read the redirection address
Fseek ($ this-> fp, $ address_offset); // locate the pointer to the redirected address
$ Flag = $ this-> getflag (); // read the flag byte
Switch (ord ($ flag )){
Case 2: // region 1 is redirected again,
Fseek ($ this-> fp, $ this-> getoffset ());
$ Address ["area1"] = $ this-> getstr ();
Fseek ($ this-> fp, $ address_offset + 4); // 4 bytes
$ Address ["area2"] = $ this-> readaddress (); // region 2 may be redirected, but may not
Break;
Default: // region 1, Region 2 are not redirected
Fseek ($ this-> fp, $ address_offset); // locate the pointer to the redirected address
$ Address ["area1"] = $ this-> getstr ();
$ Address ["area2"] = $ this-> readaddress ();
Break;
}
Break;
Case 2: // region 1 redirection region 2 no redirection
$ Address1_offset = $ this-> getoffset (); // read the redirection address
Fseek ($ this-> fp, $ address1_offset );
$ Address ["area1"] = $ this-> getstr ();
Fseek ($ this-> fp, $ address_offset + 8 );
$ Address ["area2"] = $ this-> readaddress ();
Break;
Default: // region 1 region 2 is not redirected
Fseek ($ this-> fp, $ address_offset + 4 );
$ Address ["area1"] = $ this-> getstr ();
$ Address ["area2"] = $ this-> readaddress ();
Break;
}
// * Filter useless data
If (strpos ($ address ["area1"], "cz88.net ")! = False ){
$ Address ["area1"] = "unknown ";
}
If (strpos ($ address ["area2"], "cz88.net ")! = False ){
$ Address ["area2"] = "";
}
Return $ address;
}
}

/* Usage :*/
$ Ip = new iplocation ("qqwry. dat ");
$ Address = $ ip-> getaddress ("61.129.51.27 ");
// $ Address = $ ip-> getaddress (www.jb51.net );
Echo'

';
Print_r ($ address );
?>

The specific implementation of pipeline is as follows: the code is as follows :? Php/*** @ date 2010.12.21 note: the file header [offset of the first index (4 byte)] + [offset of the last index...

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.