首頁 後端開發 php教程 再次提供一個IP地理位置查詢類

再次提供一個IP地理位置查詢類

Jul 25, 2016 am 09:11 AM

IP 地理位置查詢類
  1. /**
  2. 檔名:IpLocation.class.php
  3. * IP 地理位置查詢類別(主檔案我上傳上來了還有一個測試檔案我上傳上來,同時還有一個QQWry.Dat這個大家可以在純真IP庫下載到因為有6M多所以這裡不上傳上來了)
  4. *
  5. * @author 馬秉堯
  6. * @version 1.5
  7. * @copyright 2005 CoolCode.CN
  8. */
  9. class IpLocation {
  10. /**
  11. * QQWry.Dat檔案指標
  12. * @var resource
  13. */
  14. var $fp;
  15. /**
  16. * 第一筆IP記錄的偏移位址
  17. * @var int
  18. */
  19. var $firstip;
  20. /**
  21. * 最後一筆IP記錄的偏移位址
  22. * @var int
  23. */
  24. var $lastip;
  25. * IP記錄的總條數(不包含版本資訊記錄)
  26. * @var int
  27. */
  28. var $lastip;
  29. * 建構函數,開啟 QQWry.Dat 檔案並初始化類別中的資訊
  30. * @param string $filename
  31. * @return IpLocation
  32. /**
  33. * 傳回讀取的長整數數
  34. * @access private
  35. * @return int
  36. */
  37. var $totalip;
  38. /**
  39. * 傳回讀取的3個位元組的長整數數
  40. *
  41. * @access private
  42. * @return int
  43. */
  44. function __construct($filename = "QQWry.Dat") {
  45. $ this->fp = 0;
  46. if (($this->fp = @fopen($filename, 'rb')) !== false) {
  47. $this->firstip = $this->getlong ();
  48. $this->lastip = $this->getlong();
  49. $this->totalip = ($this->lastip - $this->firstip) / 7;
  50. //註冊析構函數,使其在程式執行結束時執行
  51. register_shutdown_function(array(&$this, '__construct'));
  52. }
  53. }
  54. /**
  55. * 回傳壓縮後可比較的IP位址
  56. *
  57. * @access private
  58. * @param string $ip
  59. * @return string
  60. */
  61. function getlong() {
  62. //將讀取的little-endian編碼的4個位元組轉換為長整數數
  63. $result = unpack('Vlong', fread($this ->fp, 4));
  64. return $result['long'];
  65. }
  66. /**
  67. * 回傳讀取的字串
  68. *
  69. * @access private
  70. * @param string $data
  71. * @return string
  72. */
  73. function getlong3() {
  74. / /將讀取的little-endian編碼的3個位元組轉換為長整數數
  75. $result = unpack('Vlong', fread($this->fp, 3).chr(0));
  76. return $result['long'];
  77. }
  78. /**
  79. * 回傳地區資訊
  80. *
  81. * @access private
  82. * @return string
  83. */
  84. function packip($ip) {
  85. // 將IP位址轉換為長整數型數,如果在PHP5中,IP位址錯誤,則傳回False,
  86. // 這時intval將Flase轉換為整數-1,之後壓縮成big-endian編碼的字串
  87. return pack(' N', intval(ip2long($ip)));
  88. }
  89. /**
  90. * 根據所給予 IP 位址或網域名稱返回所在地區資訊
  91. * @access public
  92. * @param string $ip
  93. * @return array
  94. */
  95. function getstring($data = "") {
  96. $char = fread($this->fp, 1);
  97. while (ord($char) > 0) { // 字串依照C格式儲存,以
  98. if ($ip $u = $i - 1; // 將搜尋的上邊界修改為中間記錄減一
  99. } else {
  100. fseek($this->fp, $this->getlong3());
  101. $endip = strrev(fread($this->fp, 4)); // 取得中間記錄的結束IP位址
  102. if ($ip > $endip) { // 使用者的IP大於中間記錄的結束IP位址時
  103. $l = $i + 1; // 將搜尋的下邊界修改為中間記錄加一
  104. } else { // 使用者的IP在中間記錄的IP範圍內時
  105. $findip = $this->firstip + $i * 7;
  106. break; // 則表示找到結果,退出迴圈
  107. }
  108. }
  109. }
  110. //取得查找到的IP地理位置資訊
  111. fseek($this->fp, $findip);
  112. $ location['beginip'] = long2ip($this->getlong()); // 使用者IP所在範圍的開始位址
  113. $offset = $this->getlong3();
  114. fseek($this-> fp, $offset);
  115. $location['endip'] = long2ip($this->getlong()); // 使用者IP所在範圍的結束位址
  116. $byte = fread($this->fp , 1); // 標誌位元組
  117. switch (ord($byte)) {
  118. case 1: // 標誌位元組為1,表示國家和區域資訊都同時被重新導向
  119. $countryOffset = $this->getlong3(); // 重定向位址
  120. fseek($this->fp, $countryOffset);
  121. $byte = fread($this->fp, 1); // 標誌字節
  122. switch (ord($byte)) {
  123. case 2: // 標誌位元組為2,表示國家資訊再重新重新導向
  124. fseek($this->fp, $this->getlong3 ());
  125. $location['country'] = $this->getstring();
  126. fseek($this->fp, $countryOffset + 4);
  127. $location['area'] = $this->getarea();
  128. break;
  129. default: // 否則,表示國家資訊沒有被重新導向
  130. $location['country'] = $this->getstring($byte) ;
  131. $location['area'] = $this->getarea();
  132. break;
  133. }
  134. break;
  135. case 2: // 標誌位元組為2,表示國家訊息被重新導向
  136. fseek($this->fp, $this->getlong3());
  137. $location['country'] = $this->getstring();
  138. fseek($this ->fp, $offset + 8);
  139. $location['area'] = $this->getarea();
  140. break;
  141. default: // 否則,表示國家資訊沒有被重新導向
  142. $location['country'] = $this->getstring($byte);
  143. $location['area'] = $this->getarea();
  144. break;
  145. }
  146. if ($location['country'] == " CZ88.NET") { // CZ88.NET表示沒有有效資訊
  147. $location['country'] = "未知";
  148. }
  149. if ($location['area'] == " CZ88.NET") {
  150. $location['area'] = "";
  151. }
  152. return $location;
  153. }
  154. /**
  155. * 析構函數,用於在頁面執行結束後自動關閉已開啟的檔案。
  156. *
  157. */
  158. function __desctruct() {
  159. if ($this->fp) {
  160. fclose($this->fp);
  161. }
  162. fclose($this->fp);
  163. }
  164. $this->fp = 0;
  165. }
}
?>
複製程式碼
  1. require_once('IpLocation.class.php');
  2. $ip='127.0.0.1';
  3. $idADDR=new IpLocation( );
print_r($idADDR->getlocation($ip));
?>
複製程式碼
  1. /**
  2. * IP 地理位置查詢類別
  3. *
  4. * @author 馬秉堯
  5. * @version 1.5
  6. * @copyright 2005 CoolCode.CN
  7. */
  8. class IpLocation {
  9. /**
  10. * QQWry.Dat檔案指標
  11. * @var resource
  12. */
  13. var $fp;
  14. /**
  15. * 第一筆IP記錄的偏移位址
  16. * @var int
  17. */
  18. var $firstip;
  19. /**
  20. * 最後一筆IP記錄的偏移位址
  21. * @var int
  22. */
  23. var $lastip;
  24. * IP記錄的總條數(不包含版本資訊記錄)
  25. * @var int
  26. */
  27. var $lastip;
  28. * 建構函數,開啟 QQWry.Dat 檔案並初始化類別中的資訊
  29. * @param string $filename
  30. * @return IpLocation
  31. /**
  32. * 傳回讀取的長整數數
  33. * @access private
  34. * @return int
  35. */
  36. var $totalip;
  37. /**
  38. * 傳回讀取的3個位元組的長整數數
  39. *
  40. * @access private
  41. * @return int
  42. */
  43. function __construct($filename = "QQWry.Dat") {
  44. $ this->fp = 0;
  45. if (($this->fp = @fopen($filename, 'rb')) !== false) {
  46. $this->firstip = $this->getlong ();
  47. $this->lastip = $this->getlong();
  48. $this->totalip = ($this->lastip - $this->firstip) / 7;
  49. //註冊析構函數,使其在程式執行結束時執行
  50. register_shutdown_function(array(&$this, '__construct'));
  51. }
  52. }
  53. /**
  54. * 回傳壓縮後可比較的IP位址
  55. *
  56. * @access private
  57. * @param string $ip
  58. * @return string
  59. */
  60. function getlong() {
  61. //將讀取的little-endian編碼的4個位元組轉換為長整數數
  62. $result = unpack('Vlong', fread($this ->fp, 4));
  63. return $result['long'];
  64. }
  65. /**
  66. * 回傳讀取的字串
  67. *
  68. * @access private
  69. * @param string $data
  70. * @return string
  71. */
  72. function getlong3() {
  73. / /將讀取的little-endian編碼的3個位元組轉換為長整數數
  74. $result = unpack('Vlong', fread($this->fp, 3).chr(0));
  75. return $result['long'];
  76. }
  77. /**
  78. * 回傳地區資訊
  79. *
  80. * @access private
  81. * @return string
  82. */
  83. function packip($ip) {
  84. // 將IP位址轉換為長整數型數,如果在PHP5中,IP位址錯誤,則傳回False,
  85. // 這時intval將Flase轉換為整數-1,之後壓縮成big-endian編碼的字串
  86. return pack(' N', intval(ip2long($ip)));
  87. }
  88. /**
  89. * 根據所給予 IP 位址或網域名稱返回所在地區資訊
  90. * @access public
  91. * @param string $ip
  92. * @return array
  93. */
  94. function getstring($data = "") {
  95. $char = fread($this->fp, 1);
  96. while (ord($char) > 0) { // 字串依照C格式儲存,以
  97. if ($ip $u = $i - 1; // 將搜尋的上邊界修改為中間記錄減一
  98. } else {
  99. fseek($this->fp, $this->getlong3());
  100. $endip = strrev(fread($this->fp, 4)); // 取得中間記錄的結束IP位址
  101. if ($ip > $endip) { // 使用者的IP大於中間記錄的結束IP位址時
  102. $l = $i + 1; // 將搜尋的下邊界修改為中間記錄加一
  103. } else { // 使用者的IP在中間記錄的IP範圍內時
  104. $findip = $this->firstip + $i * 7;
  105. break; // 則表示找到結果,退出迴圈
  106. }
  107. }
  108. }
  109. //取得查找到的IP地理位置資訊
  110. fseek($this->fp, $findip);
  111. $ location['beginip'] = long2ip($this->getlong()); // 使用者IP所在範圍的開始位址
  112. $offset = $this->getlong3();
  113. fseek($this-> fp, $offset);
  114. $location['endip'] = long2ip($this->getlong()); // 使用者IP所在範圍的結束位址
  115. $byte = fread($this->fp , 1); // 標誌位元組
  116. switch (ord($byte)) {
  117. case 1: // 標誌位元組為1,表示國家和區域資訊都同時被重新導向
  118. $countryOffset = $this->getlong3(); // 重定向位址
  119. fseek($this->fp, $countryOffset);
  120. $byte = fread($this->fp, 1); // 標誌字節
  121. switch (ord($byte)) {
  122. case 2: // 標誌位元組為2,表示國家資訊再重新重新導向
  123. fseek($this->fp, $this->getlong3 ());
  124. $location['country'] = $this->getstring();
  125. fseek($this->fp, $countryOffset + 4);
  126. $location['area'] = $this->getarea();
  127. break;
  128. default: // 否則,表示國家資訊沒有被重新導向
  129. $location['country'] = $this->getstring($byte) ;
  130. $location['area'] = $this->getarea();
  131. break;
  132. }
  133. break;
  134. case 2: // 標誌位元組為2,表示國家訊息被重新導向
  135. fseek($this->fp, $this->getlong3());
  136. $location['country'] = $this->getstring();
  137. fseek($this ->fp, $offset + 8);
  138. $location['area'] = $this->getarea();
  139. break;
  140. default: // 否則,表示國家資訊沒有被重新導向
  141. $location['country'] = $this->getstring($byte);
  142. $location['area'] = $this->getarea();
  143. break;
  144. }
  145. if ($location['country'] == " CZ88.NET") { // CZ88.NET表示沒有有效資訊
  146. $location['country'] = "未知";
  147. }
  148. if ($location['area'] == " CZ88.NET") {
  149. $location['area'] = "";
  150. }
  151. return $location;
  152. }
  153. /**
  154. * 析構函數,用於在頁面執行結束後自動關閉已開啟的檔案。
  155. *
  156. */
  157. function __desctruct() {
  158. if ($this->fp) {
  159. fclose($this->fp);
  160. }
  161. fclose($this->fp);
  162. }
  163. $this->fp = 0;
  164. }
}
?>
複製程式碼


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1672
14
CakePHP 教程
1428
52
Laravel 教程
1332
25
PHP教程
1276
29
C# 教程
1256
24
說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? 說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? Apr 17, 2025 am 12:06 AM

在PHP中,應使用password_hash和password_verify函數實現安全的密碼哈希處理,不應使用MD5或SHA1。1)password_hash生成包含鹽值的哈希,增強安全性。 2)password_verify驗證密碼,通過比較哈希值確保安全。 3)MD5和SHA1易受攻擊且缺乏鹽值,不適合現代密碼安全。

PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? Apr 17, 2025 am 12:25 AM

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

PHP和Python:解釋了不同的範例 PHP和Python:解釋了不同的範例 Apr 18, 2025 am 12:26 AM

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP和Python:代碼示例和比較 PHP和Python:代碼示例和比較 Apr 15, 2025 am 12:07 AM

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

您如何防止PHP中的SQL注入? (準備的陳述,PDO) 您如何防止PHP中的SQL注入? (準備的陳述,PDO) Apr 15, 2025 am 12:15 AM

在PHP中使用預處理語句和PDO可以有效防範SQL注入攻擊。 1)使用PDO連接數據庫並設置錯誤模式。 2)通過prepare方法創建預處理語句,使用佔位符和execute方法傳遞數據。 3)處理查詢結果並確保代碼的安全性和性能。

PHP:處理數據庫和服務器端邏輯 PHP:處理數據庫和服務器端邏輯 Apr 15, 2025 am 12:15 AM

PHP在數據庫操作和服務器端邏輯處理中使用MySQLi和PDO擴展進行數據庫交互,並通過會話管理等功能處理服務器端邏輯。 1)使用MySQLi或PDO連接數據庫,執行SQL查詢。 2)通過會話管理等功能處理HTTP請求和用戶狀態。 3)使用事務確保數據庫操作的原子性。 4)防止SQL注入,使用異常處理和關閉連接來調試。 5)通過索引和緩存優化性能,編寫可讀性高的代碼並進行錯誤處理。

PHP的目的:構建動態網站 PHP的目的:構建動態網站 Apr 15, 2025 am 12:18 AM

PHP用於構建動態網站,其核心功能包括:1.生成動態內容,通過與數據庫對接實時生成網頁;2.處理用戶交互和表單提交,驗證輸入並響應操作;3.管理會話和用戶認證,提供個性化體驗;4.優化性能和遵循最佳實踐,提升網站效率和安全性。

在PHP和Python之間進行選擇:指南 在PHP和Python之間進行選擇:指南 Apr 18, 2025 am 12:24 AM

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

See all articles