Php query IP location (dat database)-pure IP database-PHP source code

Source: Internet
Author: User
Php query IP geographic location (dat database)-pure IP database can search and download pure IP database on the Internet, and then use the following function to call getaddress ($ ip) returns detailed geographic location information.

PHP code

Header ("content-type: text/html; charset = utf-8"); // * // File header [offset of the first index (4 bytes)] + [offset address of the last index (4 byte)] 8 bytes // Record area [end ip (4 byte)] + [Region 1] + [Region 2] 4 bytes + uncertain length // Index area [starting ip (4 byte)] + [pointing to the record area offset address (3 byte)] 7 bytes // Note: Please download the pure IP database online and change it to "CoralWry. put dat in the current directory. // query by www.query8.com//?class ipLocation {var $ fp; var $ firstip; // offset address var $ lastip of the first ip index; // the offset of the last ip index var $ totalip; // The total number of ip addresses // * // The constructor initializes some variables // $ datfi The value of le is the name of the pure IP database, which can be modified by yourself. // * function ipLocation ($ datfile = ".. /ip. dat ") {$ this-> fp = fopen ($ datfile, 'RB'); // enable $ this-> firstip = $ this-> get4b () in binary mode (); // The absolute offset of the first ip index $ this-> lastip = $ this-> get4b (); // The absolute offset 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 7-byte value, which must be divided by 7. register_shutdown_function (array ($ this, "closefp"); // to be compatible with versions earlier than php5, this class does not use Destructor to automatically close the ip library .} // * // Close the ip library // * function Closefp () {fclose ($ this-> fp);} // * // Read 4 bytes and decompress it into 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 the detailed ip address information // * function getstr () {$ split = fread ($ this-> fp, 1); $ str = ""; while (ord ($ split )! = 0) {$ str. = $ split; $ split = fread ($ this-> fp, 1);} return $ str;} // * // Convert the ip address from ip2long to an ipv4 Internet address, compress the data into the big-endian byte order // use it to compare the ip address in the index area. // * function iptoint ($ ip) {return pack ("N ", intval (ip2long ($ ip);} // * // Obtain the client ip address // Note: If you want to record the ip address to the server, check whether ip data is secure before writing to the database. // * function getIP () {if (getenv ('http _ CLIENT_IP ') {$ ip = getenv ('http _ CLIENT_IP ');} elseif (getenv ('http _ X_FORWARDED_FOR ') {// Obtain the real ip address used by the client to access the server Address $ ip = getenv ('http _ X_FORWARDED_FOR ');} elseif (getenv ('http _ x_forwarded') {$ ip = getenv ('http _ x_forwarded ');} elseif (getenv ('http _ FORWARDED_FOR ') {$ ip = getenv ('http _ FORWARDED_FOR');} elseif (getenv ('http _ forwarded ')) {$ ip = getenv ('http _ forwarded');} else {$ ip = $ _ SERVER ['remote _ ADDR '];} return $ ip ;} // * // obtain the address information // * function readaddress () {$ now_offset = ftell ($ this-> fp); // Obtain the current pointer address $ fla G = $ 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 ;} // * // Obtain the flag 1 or 2 // used to determine whether the address is redirected. // * function getflag () {return fread ($ this-> fp, 1 );} // * // use the binary search method to search for ip addresses in the index area // * function searchip ($ ip) {$ ip = gethostbyname ($ ip); // Convert the domain name To ip $ ip_offset ["ip"] = $ ip; $ ip = $ this-> iptoint ($ ip); // Convert the ip address into a growth integer $ firstip = 0; // search for the upper boundary $ lastip = $ this-> totalip; // search for the lower boundary $ ipoffset = $ this-> lastip; // initialize it as the offset of the last IP address while ($ firstip 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 (); $ end Ip = strrev (fread ($ this-> fp, 4); if ($ ip >$ endip) {$ firstip = $ I + 1 ;} else {$ ip_offset ["offset"] = $ this-> firstip + $ I * 7; break ;}} return $ ip_offset ;} // * // obtain the ip address details // * function getaddress ($ ip) {$ ip_offset = $ this-> searchip ($ ip ); // Obtain the absolute address of the ip address 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 (); // Obtain the offset address fseek ($ this-> fp, $ address_offset); // locate the record area $ address ["endip"] = long2ip ($ this-> get4b ()); // end ip address in the record area $ flag = $ this-> getflag (); // read the flag byte switch ($ flag) {case 1: // region 1 and Region 2 are redirected $ address_offset = $ this-> getoffset (); // read the redirected address fseek ($ this-> fp, $ address_offset ); // locate the pointer to the redirected address $ flag = $ this-> getflag (); // read the flag byte switch ($ flag) {c Ase 2: // region 1 is redirected again, fseek ($ this-> fp, $ this-> getoffset ()); $ address ["area1"] = $ this-> getstr (); fseek ($ this-> fp, $ address_offset + 4 ); // jump 4 bytes $ address ["area2"] = $ this-> readaddress (); // region 2 may be redirected, and there may be no break; default: // region 1 and Region 2 are not redirected to 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 to fseek ($ this-> fp, $ address_offset + 4); $ address ["area1"] = $ this-> getstr (); $ address ["area2"] = $ this-> readaddress (); break;} // * filter some useless data if (strpos ($ address ["area1"], "CZ88.NET ")! = False) {$ address ["area1"] = "unknown";} if (strpos ($ address ["area2"], "CZ88.NET ")! = False) {$ address ["area2"] = "";} return $ address ;}// * ipLocation class end
Related Article

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.