search
HomeBackend DevelopmentPHP TutorialSimple example of php reading innocent ip database

  1. /*----------------------------------------- ----------
  2. ip2address [qqwry.dat]
  3. --------------------------------- --------------------*/
  4. class ip {
  5. var $fh; //IP database file handle
  6. var $first; //First index
  7. var $last; //The last index
  8. var $total; //Total indexes
  9. //Constructor function
  10. function __construct() {
  11. $this->fh = fopen('qqwry.dat', 'rb'); / /qqwry.dat file
  12. $this->first = $this->getLong4();
  13. $this->last = $this->getLong4();
  14. $this->total = ($this ->last - $this->first) / 7; //Each index is 7 bytes
  15. }
  16. //Check the legality of the IP
  17. function checkIp($ip) {
  18. $arr = explode('.', $ip);
  19. if(count($arr) !=4 ) {
  20. return false;
  21. } else {
  22. for ($i=0; $i if ($arr[$ i] '255') {
  23. return false;
  24. }
  25. }
  26. }
  27. return true;
  28. }
  29. function getLong4() {
  30. //Read little -Convert 4 bytes of endian encoding to long integer
  31. $result = unpack('Vlong', fread($this->fh, 4));
  32. return $result['long'];
  33. }
  34. function getLong3() {
  35. //Read the 3 bytes of little-endian encoding and convert it into a long integer
  36. $result = unpack('Vlong', fread($this->fh, 3).chr(0 ));
  37. return $result['long'];
  38. }
  39. //Query information
  40. function getInfo($data = "") {
  41. $char = fread($this->fh, 1);
  42. while ( ord($char) != 0) { //Country and region information ends with 0
  43. $data .= $char;
  44. $char = fread($this->fh, 1);
  45. }
  46. return $data;
  47. } bbs.it-home.org
  48. //Query area information
  49. function getArea() {
  50. $byte = fread($this->fh, 1); //Flag byte
  51. switch (ord($byte)) {
  52. case 0: $area = ''; break; //There is no area information
  53. case 1: //The area is redirected
  54. fseek($this->fh, $this->getLong3());
  55. $ area = $this->getInfo(); break;
  56. case 2: //Area is redirected
  57. fseek($this->fh, $this->getLong3());
  58. $area = $this- >getInfo(); break;
  59. default: $area = $this->getInfo($byte); break; //The area is not redirected
  60. }
  61. return $area;
  62. }
  63. function ip2addr($ip) {
  64. if(!$this -> checkIp($ip)){
  65. return false;
  66. }
  67. $ip = pack('N', intval(ip2long($ip)));
  68. //Binary search
  69. $ l = 0;
  70. $r = $this->total;
  71. while($l $m = floor(($l + $r) / 2); //Calculate the intermediate index
  72. fseek($this->fh, $this->first + $m * 7);
  73. $beginip = strrev(fread($this->fh, 4)); //The starting IP address of the intermediate index
  74. fseek($this->fh, $this->getLong3());
  75. $endip = strrev(fread($this->fh, 4)); //End IP address of the intermediate index
  76. if ($ ip $r = $m - 1;
  77. } else {
  78. if ($ip > $endip) { //The user's IP is greater than When the ending IP address of the intermediate index is
  79. $l = $m + 1;
  80. } else { //When the user IP is within the IP range of the intermediate index
  81. $findip = $this->first + $m * 7;
  82. break ;
  83. }
  84. }
  85. }
  86. //Query country and region information
  87. fseek($this->fh, $findip);
  88. $location['beginip'] = long2ip($this->getLong4()); / /Start address of the user IP range
  89. $offset = $this->getlong3();
  90. fseek($this->fh, $offset);
  91. $location['endip'] = long2ip($this-> ;getLong4()); //End address of the user IP range
  92. $byte = fread($this->fh, 1); //Flag byte
  93. switch (ord($byte)) {
  94. case 1: //Both country and region information are redirected
  95. $countryOffset = $this->getLong3(); //Redirect address
  96. fseek($this->fh, $countryOffset);
  97. $byte = fread($this ->fh, 1); //Flag byte
  98. switch (ord($byte)) {
  99. case 2: //Country information is redirected twice
  100. fseek($this->fh, $this-> ;getLong3());
  101. $location['country'] = $this->getInfo();
  102. fseek($this->fh, $countryOffset + 4);
  103. $location['area'] = $ this->getArea();
  104. break;
  105. default: //Country information is not redirected twice
  106. $location['country'] = $this->getInfo($byte);
  107. $location['area '] = $this->getArea();
  108. break;
  109. }
  110. break;
  111. case 2: //Country information is redirected
  112. fseek($this->fh, $this->getLong3()) ;
  113. $location['country'] = $this->getInfo();
  114. fseek($this->fh, $offset + 8);
  115. $location['area'] = $this->getArea ();
  116. break;
  117. default: //Country information is not redirected
  118. $location['country'] = $this->getInfo($byte);
  119. $location['area'] = $this-> ;getArea();
  120. break;
  121. }
  122. //gb2312 to utf-8 (remove CZ88.NET displayed when there is no information)
  123. foreach ($location as $k => $v) {
  124. $location[$k] = str_replace('CZ88.NET', '',iconv('gb2312', 'utf-8', $v));
  125. }
  126. return $location;
  127. }
  128. //Destructor
  129. function __destruct() {
  130. fclose($this->fh );
  131. }
  132. }
  133. $ip = new ip();
  134. $addr = $ip -> ip2addr('IP address');
  135. print_r($addr);
  136. ?>
Copy code


Statement
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
PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 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 in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 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.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 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.

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.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment