IP geographical location query class
- /**
- File name: IpLocation.class.php
- * IP geographical location query class (I uploaded the main file and a test file. There is also QQWry.Dat, which you can download from the Innocence IP library because there is It’s over 6M so I won’t upload it here)
- *
- * @author Ma Bingyao
- * @version 1.5
- * @copyright 2005 CoolCode.CN
- */
- class IpLocation {
- /**
- * QQWry.Dat file pointer
- * @var resource
- */
- var $fp;
-
- /**
- * Offset address of the first IP record
- * @var int
- */
- var $firstip ;
-
- /**
- * Offset address of the last IP record
- * @var int
- */
- var $lastip;
-
- /**
- * Total number of IP records (excluding version information records)
- * @var int
- */
- var $totalip;
- /**
- * Constructor, open the QQWry.Dat file and initialize the information in the class
- * @param string $filename
- * @return IpLocation
- */
- 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 is executed at the end of program execution
- register_shutdown_function(array(&$this, '__construct'));
- }
- }
-
- /**
- * Return the read long integer
- * @access private
- * @return int
- */
- function getlong() {
- //Convert the read 4 bytes of little-endian encoding into a long integer
- $result = unpack('Vlong', fread($this->fp, 4));
- return $result['long'];
- }
-
- /**
- * Returns the read 3-byte long integer
- *
- * @access private
- * @return int
- */
- 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 compressed IP address that can be compared
- *
- * @access private
- * @param string $ip
- * @return string
- */
- function packip($ip) {
- // Convert the IP address into a long integer. If the IP address is wrong in PHP5, False will be returned.
- // At this time, intval will convert the Flese into an integer -1, and then compress it into big-endian encoded string
- return pack('N', intval(ip2long($ip)));
- }
-
- /**
- * Return the read string
- *
- * @access private
- * @param string $data
- * @return string
- */
- function getstring($data = "") {
- $ char = fread($this->fp, 1);
- while (ord($char) > 0) { // The string is saved in C format, in
- 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; // It means the result is found and the loop is exited
- }
- }
- }
-
- //Get The IP geographical location information found
- fseek($this->fp, $findip);
- $location['beginip'] = long2ip($this->getlong()); // The beginning of the range where the user IP is located Address
- $offset = $this->getlong3();
- fseek($this->fp, $offset);
- $location['endip'] = long2ip($this->getlong()); / / End address of the user IP range
- $byte = fread($this->fp, 1); // Flag byte
- switch (ord($byte)) {
- case 1: // Flag byte is 1 , indicating that both country and regional 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 the country information has been 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 the country information is not redirected
- $location['country'] = $this->getstring($byte );
- $location['area'] = $this->getarea();
- break;
- }
- break;
- case 2: // The flag byte is 2, indicating that the 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 is not 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;
- }
-
- /**
- * Destructor, used to automatically close the open file after the page execution ends.
- *
- */
- function __desctruct() {
- if ($this->fp) {
- fclose($this->fp);
- }
- $this->fp = 0;
- }
- }
- ?>
Copy code
- require_once('IpLocation.class.php');
- $ip='127.0.0.1';
- $idADDR=new IpLocation();
- print_r($idADDR->getlocation($ ip));
- ?>
Copy code
- /**
- * IP geographical location query class
- *
- * @author Ma Bingyao
- * @version 1.5
- * @copyright 2005 CoolCode.CN
- */
- class IpLocation {
- /**
- * QQWry.Dat file pointer
- * @var resource
- */
- var $fp;
-
- /**
- * Offset address of the first IP record
- * @var int
- */
- var $firstip ;
-
- /**
- * Offset address of the last IP record
- * @var int
- */
- var $lastip;
-
- /**
- * Total number of IP records (excluding version information records)
- * @var int
- */
- var $totalip;
- /**
- * Constructor, open the QQWry.Dat file and initialize the information in the class
- * @param string $filename
- * @return IpLocation
- */
- 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 is executed at the end of program execution
- register_shutdown_function(array(&$this, '__construct'));
- }
- }
-
- /**
- * Return the read long integer
- * @access private
- * @return int
- */
- function getlong() {
- //Convert the read 4 bytes of little-endian encoding into a long integer
- $result = unpack('Vlong', fread($this->fp, 4));
- return $result['long'];
- }
-
- /**
- * Returns the read 3-byte long integer
- *
- * @access private
- * @return int
- */
- 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 compressed IP address that can be compared
- *
- * @access private
- * @param string $ip
- * @return string
- */
- function packip($ip) {
- // Convert the IP address into a long integer. If the IP address is wrong in PHP5, False will be returned.
- // At this time, intval will convert the Flese into an integer -1, and then compress it into big-endian encoded string
- return pack('N', intval(ip2long($ip)));
- }
-
- /**
- * Return the read string
- *
- * @access private
- * @param string $data
- * @return string
- */
- function getstring($data = "") {
- $ char = fread($this->fp, 1);
- while (ord($char) > 0) { // The string is saved in C format, in
- 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; // It means the result is found and the loop is exited
- }
- }
- }
-
- //Get The IP geographical location information found
- fseek($this->fp, $findip);
- $location['beginip'] = long2ip($this->getlong()); // The beginning of the range where the user IP is located Address
- $offset = $this->getlong3();
- fseek($this->fp, $offset);
- $location['endip'] = long2ip($this->getlong()); / / End address of the user IP range
- $byte = fread($this->fp, 1); // Flag byte
- switch (ord($byte)) {
- case 1: // Flag byte is 1 , indicating that both country and regional 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 the country information has been 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 the country information is not redirected
- $location['country'] = $this->getstring($byte );
- $location['area'] = $this->getarea();
- break;
- }
- break;
- case 2: // The flag byte is 2, indicating that the 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 is not 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;
- }
-
- /**
- * Destructor, used to automatically close the open file after the page execution ends.
- *
- */
- function __desctruct() {
- if ($this->fp) {
- fclose($this->fp);
- }
- $this->fp = 0;
- }
- }
- ?>
Copy code
|