pk10赛车计划软件百度云

Fuzhou website construction only needs 1,000 yuan including domain name space, details 18558752805

  • PHP uses pure IP database to implement IP address information query locally

Fuzhou Website Construction > Site News > PHP Technology >

Suggest a local IP address database. Please go to http://www.cz88.net/ to download a pure IP database. After installation, go to the installation directory and remove the QQWry.dat file. This is the IP database we want. Now, put it in the directory you want.

This IP address query processing class

 <? php / ** * IP geolocation class * / class Helper_IpLocation {/ ** * QQWry.Dat file pointer * * @var resource * / private $ fp; / ** * Offset address of the first IP record * * @var int * / private $ firstip; / ** * Offset address of the last IP record * * @var int * / private $ lastip; / ** * Total number of IP records (excluding version information records ) * * @Var int * / private $ totalip; / ** * Constructor, open the QQWry.Dat file and initialize the information in the class * * @param string $ filename * / public function __construct ($ 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 so that it ends at the end of program execution carried out    register_shutdown_function (array (& $ this, '__destruct'));}} / ** * Destructor, used to automatically close open files after the page has finished executing.  * * / public function __destruct () {if ($ this-> fp) {fclose ($ this-> fp);} $ this-> fp = 0;} / ** * Returns the number of long integers read * * @access private * @return int * / private function getlong () {// Convert the read 4-byte encoded little-endian into a long integer $ result = unpack ('Vlong', fread ($ this -> fp, 4)); return $ result ['long'];} / ** * Returns a 3 byte long integer read * * @access private * @return int * / private function getlong3 ( ) {// Convert the read 3 bytes of little-endian encoding into a long integer $ result = unpack ('Vlong', fread ($ this-> fp, 3). Chr (0)); return $ result ['long'];} / ** * Returns the IP address that can be compared after compression * * @access private * @param string $ ip * @return string * / private function packip ($ ip) {// will IP address converted to long  Type number, if the IP address is wrong in PHP5, it returns False, // then intval converts Flase to integer -1, and then compresses it into a big-endian encoded string return pack ('N', intval (ip2long ( $ ip)));} / ** * Returns the read string * * @access private * @param string $ data * @return string * / private function getstring ($ data = "") {$ char = fread ( $ this-> fp, 1); while (ord ($ char)> 0) {// string is saved in C format to end $ data. = $ char; // concatenate the read character to the given character $ Char = fread ($ this-> fp, 1);} return iconv ('gbk', 'utf-8', $ data);} / ** * return locale information * * @access private * @return string * / private function getarea () {$ byte = fread ($ this-> fp, 1); // flag byte switch (ord ($ byte)) {case 0: // no area information $ area = "" ; break;  case 1: case 2: // If the flag byte is 1 or 2, it means that the area information is redirected fseek ($ this-> fp, $ this-> getlong3 ()); $ area = $ this-> getstring (); break; default: // Otherwise, it means that the area information has not been redirected. $ area = $ this-> getstring ($ byte); break;} return $ area;} / ** * Returns the area based on the given IP address or domain name Message * * @access public * @param string $ ip * @return array * / public function getlocation ($ ip) {if (! $ This-> fp) return null; // if the data file is not opened correctly, then directly Returns empty $ location ['ip'] = gethostbyname ($ ip); // convert the entered domain name to an IP address $ ip = $ this-> packip ($ location ['ip']); // will enter the entered IP The address is converted to a comparable IP address // An illegal IP address is converted to 255.255.255.255 // search by half $ l = 0; // lower bound of search  $ u = $ this-> totalip; // upper bound of search $ findip = $ this-> lastip; // if not found return the last IP record (QQWry.Dat version information) while ($ l <= $ u) {// When the upper boundary is smaller than the lower boundary, the search fails $ i = floor (($ l + $ u) / 2); // calculate the approximate intermediate record fseek ($ this-> fp, $ this-> firstip + $ i * 7); $ beginip = strrev (fread ($ this-> fp, 4)); // Get the starting IP address of the intermediate record // The function of strrev here is to compress the little-endian compressed IP address Converted to big-endian format // for comparison, the same later.  if ($ ip <$ beginip) {// When the user's IP is less than the starting IP address of the intermediate record $ u = $ i-1; // Modify the upper boundary of the search to the intermediate record minus one} else {fseek ($ this -> fp, $ this-> getlong3 ()); $ endip = strrev (fread ($ this-> fp, 4)); // Get the end IP address of the intermediate record if ($ ip> $ endip) {// When the user's IP is greater than the end IP address of the intermediate record $ l = $ i + 1; // Modify the lower boundary of the search to the intermediate record plus one} else {// When the user's IP is within the IP range of the intermediate record $ findip = $ this-> firstip + $ i * 7; break; // indicates that the result was found and exit the loop}}} // Get the IP location information found fseek ($ this-> fp, $ findip); $ location ['beginip'] = long2ip ($ this-> getlong ()); // start address of the user IP range $ offset = $ this-> getlong3 (); fseek ($ this-> fp, $ offset); $ location ['endi  p '] = long2ip ($ this-> getlong ()); // end address of user IP range $ byte = fread ($ this-> fp, 1); // flag byte switch (ord ($ byte) ) {case 1: // flag byte is 1, indicating that country and area information are redirected at the same time $ countryOffset = $ this-> getlong3 (); // redirect address fseek ($ this-> fp, $ countryOffset) ; $ byte = fread ($ this-> fp, 1); // flag byte switch (ord ($ byte)) {case 2: // flag byte is 2, indicating that country information is redirected again fseek ($ this-> fp, $ this-> getlong3 ()); $ location ['country'] = $ this-> getstring (); fseek ($ this-> fp, $ countryOffset + 4); $ location ['area' ] = $ this-> getarea (); break; default: // Otherwise, it means that country information has not been redirected $ location ['country'] = $ this-> getstring ($ byte);  $ location ['area'] = $ this-> getarea (); break;} break; case 2: // flag byte is 2, indicating that country information is redirected fseek ($ this-> fp, $ this-> getlong3 ()); $ location ['country'] = $ this-> getstring (); fseek ($ this-> fp, $ offset + 8); $ location ['area'] = $ this-> getarea () break; default: // Otherwise, it means that the country information has not been redirected. $ location ['country'] = $ this-> getstring ($ byte); $ location ['area'] = $ this-> getarea (); break;} if ($ location ['country'] == "CZ88.NET") {// CZ88.NET indicates that there is no valid information $ location ['country'] = "Unknown";} if ($ location ['area '] == "CZ88.NET") {$ location [' area '] = "";} return $ location;}} 

Calling method:

<? php

include 'IpLocation.php';

echo "<pre>";
$ ip = "180.76.6.130";
$ iplocation = new IpLocation ();
$ location = $ iplocation-> getlocation ($ ip);
print_r ($ location);

Reprinted address: http://blog.csdn.net/myweishanli/article/details/45098693

Scan QR code to share on WeChat
confirm

Copyright © 2014-2019 buxie520.com Fuzhou Website Production