Home Backend Development PHP Tutorial Provide an IP geographical location query class again

Provide an IP geographical location query class again

Jul 25, 2016 am 09:11 AM

IP geographical location query class
  1. /**
  2. File name: IpLocation.class.php
  3. * 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)
  4. *
  5. * @author Ma Bingyao
  6. * @version 1.5
  7. * @copyright 2005 CoolCode.CN
  8. */
  9. class IpLocation {
  10. /**
  11. * QQWry.Dat file pointer
  12. * @var resource
  13. */
  14. var $fp;
  15. /**
  16. * Offset address of the first IP record
  17. * @var int
  18. */
  19. var $firstip ;
  20. /**
  21. * Offset address of the last IP record
  22. * @var int
  23. */
  24. var $lastip;
  25. /**
  26. * Total number of IP records (excluding version information records)
  27. * @var int
  28. */
  29. var $totalip;
  30. /**
  31. * Constructor, open the QQWry.Dat file and initialize the information in the class
  32. * @param string $filename
  33. * @return IpLocation
  34. */
  35. function __construct($filename = "QQWry .Dat") {
  36. $this->fp = 0;
  37. if (($this->fp = @fopen($filename, 'rb')) !== false) {
  38. $this->firstip = $this->getlong();
  39. $this->lastip = $this->getlong();
  40. $this->totalip = ($this->lastip - $this->firstip) / 7;
  41. //Register the destructor so that it is executed at the end of program execution
  42. register_shutdown_function(array(&$this, '__construct'));
  43. }
  44. }
  45. /**
  46. * Return the read long integer
  47. * @access private
  48. * @return int
  49. */
  50. function getlong() {
  51. //Convert the read 4 bytes of little-endian encoding into a long integer
  52. $result = unpack('Vlong', fread($this->fp, 4));
  53. return $result['long'];
  54. }
  55. /**
  56. * Returns the read 3-byte long integer
  57. *
  58. * @access private
  59. * @return int
  60. */
  61. function getlong3() {
  62. //Convert the read 3 bytes of little-endian encoding into a long integer
  63. $result = unpack('Vlong', fread($this->fp, 3).chr(0));
  64. return $result['long'];
  65. }
  66. /**
  67. * Returns the compressed IP address that can be compared
  68. *
  69. * @access private
  70. * @param string $ip
  71. * @return string
  72. */
  73. function packip($ip) {
  74. // Convert the IP address into a long integer. If the IP address is wrong in PHP5, False will be returned.
  75. // At this time, intval will convert the Flese into an integer -1, and then compress it into big-endian encoded string
  76. return pack('N', intval(ip2long($ip)));
  77. }
  78. /**
  79. * Return the read string
  80. *
  81. * @access private
  82. * @param string $data
  83. * @return string
  84. */
  85. function getstring($data = "") {
  86. $ char = fread($this->fp, 1);
  87. while (ord($char) > 0) { // The string is saved in C format, in
  88. if ($ip < $beginip) { // When the user's IP is less than the starting IP address of the intermediate record
  89. $u = $i - 1; // Modify the upper boundary of the search to the intermediate record minus one
  90. } else {
  91. fseek($this->fp, $this->getlong3());
  92. $endip = strrev(fread($this->fp, 4)); // Get the end IP address of the intermediate record
  93. if ($ip > $endip) { // When the user's IP is greater than the end IP address of the intermediate record
  94. $l = $i + 1; // Modify the lower boundary of the search to the intermediate record plus one
  95. } else { // When the user's IP is within the IP range of the intermediate record
  96. $findip = $this->firstip + $i * 7;
  97. break; // It means the result is found and the loop is exited
  98. }
  99. }
  100. }
  101. //Get The IP geographical location information found
  102. fseek($this->fp, $findip);
  103. $location['beginip'] = long2ip($this->getlong()); // The beginning of the range where the user IP is located Address
  104. $offset = $this->getlong3();
  105. fseek($this->fp, $offset);
  106. $location['endip'] = long2ip($this->getlong()); / / End address of the user IP range
  107. $byte = fread($this->fp, 1); // Flag byte
  108. switch (ord($byte)) {
  109. case 1: // Flag byte is 1 , indicating that both country and regional information are redirected at the same time
  110. $countryOffset = $this->getlong3(); // Redirect address
  111. fseek($this->fp, $countryOffset);
  112. $byte = fread($ this->fp, 1); // Flag byte
  113. switch (ord($byte)) {
  114. case 2: // Flag byte is 2, indicating that the country information has been redirected again
  115. fseek($this-> ;fp, $this->getlong3());
  116. $location['country'] = $this->getstring();
  117. fseek($this->fp, $countryOffset + 4);
  118. $location ['area'] = $this->getarea();
  119. break;
  120. default: // Otherwise, it means that the country information is not redirected
  121. $location['country'] = $this->getstring($byte );
  122. $location['area'] = $this->getarea();
  123. break;
  124. }
  125. break;
  126. case 2: // The flag byte is 2, indicating that the country information is redirected
  127. fseek($ this->fp, $this->getlong3());
  128. $location['country'] = $this->getstring();
  129. fseek($this->fp, $offset + 8);
  130. $location['area'] = $this->getarea();
  131. break;
  132. default: // Otherwise, it means that the country information is not redirected
  133. $location['country'] = $this->getstring ($byte);
  134. $location['area'] = $this->getarea();
  135. break;
  136. }
  137. if ($location['country'] == " CZ88.NET") { // CZ88 .NET indicates that there is no valid information
  138. $location['country'] = "Unknown";
  139. }
  140. if ($location['area'] == " CZ88.NET") {
  141. $location['area'] = " ";
  142. }
  143. return $location;
  144. }
  145. /**
  146. * Destructor, used to automatically close the open file after the page execution ends.
  147. *
  148. */
  149. function __desctruct() {
  150. if ($this->fp) {
  151. fclose($this->fp);
  152. }
  153. $this->fp = 0;
  154. }
  155. }
  156. ?>
Copy code
  1. require_once('IpLocation.class.php');
  2. $ip='127.0.0.1';
  3. $idADDR=new IpLocation();
  4. print_r($idADDR->getlocation($ ip));
  5. ?>
Copy code
  1. /**
  2. * IP geographical location query class
  3. *
  4. * @author Ma Bingyao
  5. * @version 1.5
  6. * @copyright 2005 CoolCode.CN
  7. */
  8. class IpLocation {
  9. /**
  10. * QQWry.Dat file pointer
  11. * @var resource
  12. */
  13. var $fp;
  14. /**
  15. * Offset address of the first IP record
  16. * @var int
  17. */
  18. var $firstip ;
  19. /**
  20. * Offset address of the last IP record
  21. * @var int
  22. */
  23. var $lastip;
  24. /**
  25. * Total number of IP records (excluding version information records)
  26. * @var int
  27. */
  28. var $totalip;
  29. /**
  30. * Constructor, open the QQWry.Dat file and initialize the information in the class
  31. * @param string $filename
  32. * @return IpLocation
  33. */
  34. function __construct($filename = "QQWry .Dat") {
  35. $this->fp = 0;
  36. if (($this->fp = @fopen($filename, 'rb')) !== false) {
  37. $this->firstip = $this->getlong();
  38. $this->lastip = $this->getlong();
  39. $this->totalip = ($this->lastip - $this->firstip) / 7;
  40. //Register the destructor so that it is executed at the end of program execution
  41. register_shutdown_function(array(&$this, '__construct'));
  42. }
  43. }
  44. /**
  45. * Return the read long integer
  46. * @access private
  47. * @return int
  48. */
  49. function getlong() {
  50. //Convert the read 4 bytes of little-endian encoding into a long integer
  51. $result = unpack('Vlong', fread($this->fp, 4));
  52. return $result['long'];
  53. }
  54. /**
  55. * Returns the read 3-byte long integer
  56. *
  57. * @access private
  58. * @return int
  59. */
  60. function getlong3() {
  61. //Convert the read 3 bytes of little-endian encoding into a long integer
  62. $result = unpack('Vlong', fread($this->fp, 3).chr(0));
  63. return $result['long'];
  64. }
  65. /**
  66. * Returns the compressed IP address that can be compared
  67. *
  68. * @access private
  69. * @param string $ip
  70. * @return string
  71. */
  72. function packip($ip) {
  73. // Convert the IP address into a long integer. If the IP address is wrong in PHP5, False will be returned.
  74. // At this time, intval will convert the Flese into an integer -1, and then compress it into big-endian encoded string
  75. return pack('N', intval(ip2long($ip)));
  76. }
  77. /**
  78. * Return the read string
  79. *
  80. * @access private
  81. * @param string $data
  82. * @return string
  83. */
  84. function getstring($data = "") {
  85. $ char = fread($this->fp, 1);
  86. while (ord($char) > 0) { // The string is saved in C format, in
  87. if ($ip < $beginip) { // When the user's IP is less than the starting IP address of the intermediate record
  88. $u = $i - 1; // Modify the upper boundary of the search to the intermediate record minus one
  89. } else {
  90. fseek($this->fp, $this->getlong3());
  91. $endip = strrev(fread($this->fp, 4)); // Get the end IP address of the intermediate record
  92. if ($ip > $endip) { // When the user's IP is greater than the end IP address of the intermediate record
  93. $l = $i + 1; // Modify the lower boundary of the search to the intermediate record plus one
  94. } else { // When the user's IP is within the IP range of the intermediate record
  95. $findip = $this->firstip + $i * 7;
  96. break; // It means the result is found and the loop is exited
  97. }
  98. }
  99. }
  100. //Get The IP geographical location information found
  101. fseek($this->fp, $findip);
  102. $location['beginip'] = long2ip($this->getlong()); // The beginning of the range where the user IP is located Address
  103. $offset = $this->getlong3();
  104. fseek($this->fp, $offset);
  105. $location['endip'] = long2ip($this->getlong()); / / End address of the user IP range
  106. $byte = fread($this->fp, 1); // Flag byte
  107. switch (ord($byte)) {
  108. case 1: // Flag byte is 1 , indicating that both country and regional information are redirected at the same time
  109. $countryOffset = $this->getlong3(); // Redirect address
  110. fseek($this->fp, $countryOffset);
  111. $byte = fread($ this->fp, 1); // Flag byte
  112. switch (ord($byte)) {
  113. case 2: // Flag byte is 2, indicating that the country information has been redirected again
  114. fseek($this-> ;fp, $this->getlong3());
  115. $location['country'] = $this->getstring();
  116. fseek($this->fp, $countryOffset + 4);
  117. $location ['area'] = $this->getarea();
  118. break;
  119. default: // Otherwise, it means that the country information is not redirected
  120. $location['country'] = $this->getstring($byte );
  121. $location['area'] = $this->getarea();
  122. break;
  123. }
  124. break;
  125. case 2: // The flag byte is 2, indicating that the country information is redirected
  126. fseek($ this->fp, $this->getlong3());
  127. $location['country'] = $this->getstring();
  128. fseek($this->fp, $offset + 8);
  129. $location['area'] = $this->getarea();
  130. break;
  131. default: // Otherwise, it means that the country information is not redirected
  132. $location['country'] = $this->getstring ($byte);
  133. $location['area'] = $this->getarea();
  134. break;
  135. }
  136. if ($location['country'] == " CZ88.NET") { // CZ88 .NET indicates that there is no valid information
  137. $location['country'] = "Unknown";
  138. }
  139. if ($location['area'] == " CZ88.NET") {
  140. $location['area'] = " ";
  141. }
  142. return $location;
  143. }
  144. /**
  145. * Destructor, used to automatically close the open file after the page execution ends.
  146. *
  147. */
  148. function __desctruct() {
  149. if ($this->fp) {
  150. fclose($this->fp);
  151. }
  152. $this->fp = 0;
  153. }
  154. }
  155. ?>
Copy code


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles