Php reads the class instance code of the qqwry. dat IP address location file,
Example:
<? Php // + response // | // + ---------------------------------------------------------------------------- // | // + specify class iplocate {var $ fp; var $ firstip; // The offset of the first ip index var $ lastip; // the offset of the last ip index var $ totalip; // The total number of ip addresses // obtain the Client ip address function get_client_ip () {if (getenv ("HTTP_CLIENT_IP ")&& Strcasecmp (getenv ("HTTP_CLIENT_IP"), "unknown") $ ip = getenv ("HTTP_CLIENT_IP"); else if (getenv ("HTTP_X_FORWARDED_FOR ") & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR"), "unknown") $ ip = getenv ("HTTP_X_FORWARDED_FOR"); else if (getenv ("REMOTE_ADDR ") & strcasecmp (getenv ("REMOTE_ADDR"), "unknown") $ ip = getenv ("REMOTE_ADDR "); else if (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR'] & strcasecmp ($ _ SERVER ['remote _ ADDR '], "unknown") $ ip = $ _ SERVER ['remote _ ADDR']; else $ ip = "unknown "; return ($ ip) ;}//* // constructor, initialize some variables // $ datfile value is the name of the pure IP database, you can modify it on your own. // * function iplocate () {$ datfile = "ip1.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. The index area is 7 characters in length. Section, divided by 7, register_shutdown_function (array ($ this, "closefp"); // to be compatible with versions earlier than php5, this class does not use destructor, automatically shut down the ip library .} // * // close the ip address library // * function closefp () {fclose ($ this-> fp );} /// * // read the four bytes and decompress them into the long mode of long. // * 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); 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 when the client is accessed by a proxy server $ 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 address information // * function readaddress () {$ now_offset = ftell ($ this-> fp ); // obtain the current pointer address $ flag = $ this-> getflag (); sw Itch (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 // 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-> EPT Oint ($ ip); // convert the ip 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 address of 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, fseek ($ this-> fp, $ this-> firstip + $ I * 7) is also available in four homes and five homes ); // 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 the 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 (); // obtain the offset fseek ($ this-> fp, $ address_offset) of the ip address in the ip record area in the index area ); // locate $ address ["endip"] = long2ip ($ this-> get4b () in the record area ()); // 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 Mark 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 ); // 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"] = "";} foreach ($ address as $ k => $ item) {if (! $ This-> is_utf8 ($ address [$ k]) {$ address [$ k] = iconv ('gbk', 'utf-8', $ item );}} return $ address;} function is_utf8 ($ string) {return preg_match ('% ^ (?: [\ X09 \ x0A \ x0D \ x20-\ x7E] # ASCII | [\ xC2-\ xDF] [\ x80-\ xBF] # non-overlong 2-byte | \ xE0 [\ xA0-\ xBF] [\ x80-\ xBF] # excluding overlongs | [\ xE1-\ xEC \ xEE \ xEF] [\ x80-\ xBF] {2 }# straight 3-byte | \ xED [\ x80-\ x9F] [\ x80-\ xBF] # excluding surrogates | \ xF0 [\ x90-\ xBF] [\ x80-\ xBF] {2} # planes 1-3 | [\ xF1-\ xF3] [\ x80-\ xBF] {3} # planes 4-15 | \ xF4 [\ x80-\ x8F] [\ x80-\ xBF] {2} # plane 16) * $ % xs ', $ string) ;}}?>
The above example code of the php class that reads the qqwry. dat IP address location file is all the content that I have shared with you. I hope you can give me a reference and support me a lot.