PHP obtains the IP address location information (using the pure IP address database qqwry. dat), ipqqwry. dat
As follows:
<? Php $ data = '2017. 254.254.254 '; echo ip2long ($ data); function getIP () {if (getenv ("HTTP_CLIENT_IP") & strcasecmp (getenv ("HTTP_CLIENT_IP"), "unknown ")) $ ip = getenv ("HTTP_CLIENT_IP"); elseif (getenv ("HTTP_X_FORWARDED_FOR") & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR"), "unknown ")) $ ip = getenv ("HTTP_X_FORWARDED_FOR"); elseif (getenv ("REMOTE_ADDR") & strcasecmp (getenv ("REMOTE_ADDR"), "unknown") $ ip = Getenv ("REMOTE_ADDR"); elseif (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], "unknown ")) $ ip = $ _ SERVER ['remote _ ADDR ']; else $ ip = "0.0.0.0"; return $ ip;} class IpLocation {// data file pointer var $ fp; var $ firstip; var $ lastip; var $ totalip; function getlong () {// unpack unpacks data from binary strings // converts the four bytes of the read little-endian encoding into long integer data, fread securely reads binary files $ result = unpack ('vlo' Ng ', fread ($ this-> fp, 4); return $ result ['long'];} function getlong3 () {// convert the three bytes of the read little-endian encoding into the number of long integers $ result = unpack ('vlong', fread ($ this-> fp, 3 ). chr (0); return $ result ['long'];} function packip ($ ip) {// pack load data into a binary string // ip2long converts the IP address to an unsigned long integer. It can also be used to verify the IP address return pack ('n ', intval (ip2long ($ ip);} function getstring ($ data = "") {$ char = fread ($ this-> fp, 1 ); while (ord ($ char)> 0 ){// Ord returns the ASCII value of the character. The string is saved in C format and ends with \ 0 $ data. = $ char; $ char = fread ($ this-> fp, 1);} return $ data;} function getarea () {$ byte = fread ($ this-> fp, 1); // indicates the byte switch (ord ($ byte) {case 0: // No region information $ area = ""; break; case 1: case 2: // The flag byte is 1 or 2, indicating that the region information is redirected to fseek ($ this-> fp, $ this-> getlong3 ()); $ area = $ this-> getstring (); break; default: // otherwise, the region information is not redirected $ area = $ this-> getstring ($ byte ); break;} return $ Area;} function getlocation ($ ip) {if (! $ This-> fp) return null; // if the data file is not properly opened, a blank $ location ['IP'] = gethostbyname ($ ip) is returned ); // convert the domain name to an ip address $ ip = $ this-> packip ($ location ['IP']); // convert the Entered IP address to a comparable IP address // an invalid IP address will be converted to 255 // you can search for $ l = 0; // search for the bottom boundary $ u = $ this-> totalip; // search for the top boundary $ findip = $ this-> lastip; // if not found, return the last IP record (QQWry. dat version information) while ($ l <= $ u) {// when the boundary is smaller than the bottom boundary, the query fails. $ I = floor ($ l + $ u) /2); // calculate the approximate intermediate record fseek ($ this-> fp, $ this-> f Irstip + $ I * 7); $ beginip = strrev (fread ($ this-> fp, 4); // obtain the starting IP address of the intermediate record, strrev reverse string // The strrev function is used to convert the compressed IP address of little-endian to the big-endian format, easy to compare // for little-endian and big-endian references: http://baike.baidu.com/view/2368412.htm if ($ ip <$ beginip) {// when the user's IP address is less than the starting IP address of the intermediate record, $ u = $ I-1; // change the upper boundary of the search to an intermediate record minus one} else {fseek ($ this-> fp, $ this-> getlong3 ()); $ endip = strrev (fread ($ this-> fp, 4); // obtain the end IP address of the intermediate record I F ($ ip> $ endip) {// when the user's IP address is greater than the end ip address of the intermediate record, $ l = $ I + 1; // change the bottom boundary of the search to an intermediate record plus} else {// when the user's IP address is within the IP Range of the intermediate record $ findip = $ this-> firstip + $ I * 7; break; // indicates that the result is found and the loop is exited.} fseek ($ this-> fp, $ findip ); $ location ['ininip'] = long2ip ($ this-> getlong (); // start address of the user's IP Range $ offset = $ this-> getlong3 (); fseek ($ this-> fp, $ offset); $ location ['enabled'] = long2ip ($ this-> getlong ()); // end address of the user's IP address range $ byte = fread ($ t His-> fp, 1); // mark byte switch (ord ($ byte) {case 1: // mark byte is 1, indicates that the country and region information is redirected at the same time $ countryOffset = $ this-> getlong3 (); // redirect address fseek ($ this-> fp, $ countryOffset ); $ byte = fread ($ this-> fp, 1); // indicates that the byte switch (ord ($ byte) {case 2: // indicates that the byte is 2, indicates that the country information is redirected to fseek ($ this-> fp, $ this-> getlong3 (); $ location ['country'] = $ this-> getstring (); fseek ($ this-> fp, $ countryOffset + 4); $ location ['region'] = $ this-> getarea (); Break; default: // otherwise, the country information is not redirected $ location ['country'] = $ this-> getstring ($ byte ); $ location ['region'] = $ this-> getarea (); break;} break; case 2: // The flag byte is 2, indicates that the country information is redirected to fseek ($ this-> fp, $ this-> getlong3 (); $ location ['country'] = $ this-> getstring (); fseek ($ this-> fp, $ offset + 8); $ location ['region'] = $ this-> getarea (); break; default: // otherwise, the country information is not redirected $ location ['country'] = $ this-> getstring ($ byte ); $ Location ['region'] = $ this-> getarea (); break;} if ($ location ['country'] = "CZNET ") {// CZNET indicates no valid information. $ location ['country'] = "unknown";} if ($ location ['region'] = "CZNET ") {$ location ['region'] = "";} return $ location;}/*** constructor to open QQWry. dat file and initialize information in the class */function IpLocation ($ filename = "qqwry. dat ") {$ this-> fp = 0; if ($ this-> fp = @ fopen ($ filename, 'rb '))! = False) {$ this-> firstip = $ this-> getlong (); $ this-> lastip = $ this-> getlong (); $ this-> totalip = ($ this-> lastip-$ this-> firstip)/7; // register the destructor, execute register_shutdown_function (array (& $ this, '_ iplocation') ;}/ *** destructor at the end of program execution, used to automatically close open files after page execution ends */function _ IpLocation () {if ($ this-> fp) {fclose ($ this-> fp );} $ this-> fp = 0 ;}} header ("content-Type: text/html; charset = gbk"); $ ipOrDomain = '2017. 0.0.0 '; // $ ipOrDomain = 'www .baidu.com'; $ iplocation = new IpLocation (); $ location = $ iplocation-> getlocation ($ ipOrDomain ); $ address = mb_convert_encoding ($ location ['country']. $ location ['region'], "gbk", "gbk"); echo $ address;?>
The above PHP instance for obtaining IP address location information (using pure IP database qqwry. dat) is all the content shared by the editor. I hope to give you a reference and support for the customer's house.