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

來源:互聯網
上載者:User
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. /**
  26. * IP記錄的總條數(不包含版本資訊記錄)
  27. * @var int
  28. */
  29. var $totalip;
  30. /**
  31. * 建構函式,開啟 QQWry.Dat 檔案並初始化類中的資訊
  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. //註冊解構函式,使其在程式執行結束時執行
  42. register_shutdown_function(array(&$this, '__construct'));
  43. }
  44. }
  45. /**
  46. * 返回讀取的長整型數
  47. * @access private
  48. * @return int
  49. */
  50. function getlong() {
  51. //將讀取的little-endian編碼的4個位元組轉化為長整型數
  52. $result = unpack('Vlong', fread($this->fp, 4));
  53. return $result['long'];
  54. }
  55. /**
  56. * 返回讀取的3個位元組的長整型數
  57. *
  58. * @access private
  59. * @return int
  60. */
  61. function getlong3() {
  62. //將讀取的little-endian編碼的3個位元組轉化為長整型數
  63. $result = unpack('Vlong', fread($this->fp, 3).chr(0));
  64. return $result['long'];
  65. }
  66. /**
  67. * 返回壓縮後可進行比較的IP地址
  68. *
  69. * @access private
  70. * @param string $ip
  71. * @return string
  72. */
  73. function packip($ip) {
  74. // 將IP地址轉化為長整型數,如果在PHP5中,IP地址錯誤,則返回False,
  75. // 這時intval將Flase轉化為整數-1,之後壓縮成big-endian編碼的字串
  76. return pack('N', intval(ip2long($ip)));
  77. }
  78. /**
  79. * 返回讀取的字串
  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) { // 字串按照C格式儲存,以\0結束
  88. $data .= $char; // 將讀取的字元串連到給定字串之後
  89. $char = fread($this->fp, 1);
  90. }
  91. return $data;
  92. }
  93. /**
  94. * 返回地區資訊
  95. *
  96. * @access private
  97. * @return string
  98. */
  99. function getarea() {
  100. $byte = fread($this->fp, 1); // 標誌位元組
  101. switch (ord($byte)) {
  102. case 0: // 沒有地區資訊
  103. $area = "";
  104. break;
  105. case 1:
  106. case 2: // 標誌位元組為1或2,表示地區資訊被重新導向
  107. fseek($this->fp, $this->getlong3());
  108. $area = $this->getstring();
  109. break;
  110. default: // 否則,表示地區資訊沒有被重新導向
  111. $area = $this->getstring($byte);
  112. break;
  113. }
  114. return $area;
  115. }
  116. /**
  117. * 根據所給 IP 位址或網域名稱返回所在地區資訊
  118. * @access public
  119. * @param string $ip
  120. * @return array
  121. */
  122. function getlocation($ip) {
  123. if (!$this->fp) return null; // 如果資料檔案沒有被正確開啟,則直接返回空
  124. $location['ip'] = gethostbyname($ip); // 將輸入的網域名稱轉化為IP地址
  125. $ip = $this->packip($location['ip']); // 將輸入的IP地址轉化為可比較的IP地址
  126. // 不合法的IP地址會被轉化為255.255.255.255
  127. // 對分搜尋
  128. $l = 0; // 搜尋的下邊界
  129. $u = $this->totalip; // 搜尋的上邊界
  130. $findip = $this->lastip; // 如果沒有找到就返回最後一條IP記錄(QQWry.Dat的版本資訊)
  131. while ($l <= $u) { // 當上邊界小於下邊界時,尋找失敗
  132. $i = floor(($l + $u) / 2); // 計算近似中間記錄
  133. fseek($this->fp, $this->firstip + $i * 7);
  134. $beginip = strrev(fread($this->fp, 4)); // 擷取中間記錄的開始IP地址
  135. // strrev函數在這裡的作用是將little-endian的壓縮IP地址轉化為big-endian的格式
  136. // 以便用於比較,後面相同。
  137. if ($ip < $beginip) { // 使用者的IP小於中間記錄的開始IP地址時
  138. $u = $i - 1; // 將搜尋的上邊界修改為中間記錄減一
  139. } else {
  140. fseek($this->fp, $this->getlong3());
  141. $endip = strrev(fread($this->fp, 4)); // 擷取中間記錄的結束IP地址
  142. if ($ip > $endip) { // 使用者的IP大於中間記錄的結束IP地址時
  143. $l = $i + 1; // 將搜尋的下邊界修改為中間記錄加一
  144. } else { // 使用者的IP在中間記錄的IP範圍內時
  145. $findip = $this->firstip + $i * 7;
  146. break; // 則表示找到結果,退出迴圈
  147. }
  148. }
  149. }
  150. //擷取尋找到的IP地理位置資訊
  151. fseek($this->fp, $findip);
  152. $location['beginip'] = long2ip($this->getlong()); // 使用者IP所在範圍的開始地址
  153. $offset = $this->getlong3();
  154. fseek($this->fp, $offset);
  155. $location['endip'] = long2ip($this->getlong()); // 使用者IP所在範圍的結束位址
  156. $byte = fread($this->fp, 1); // 標誌位元組
  157. switch (ord($byte)) {
  158. case 1: // 標誌位元組為1,表示國家和地區資訊都被同時重新導向
  159. $countryOffset = $this->getlong3(); // 重新導向地址
  160. fseek($this->fp, $countryOffset);
  161. $byte = fread($this->fp, 1); // 標誌位元組
  162. switch (ord($byte)) {
  163. case 2: // 標誌位元組為2,表示國家資訊又被重新導向
  164. fseek($this->fp, $this->getlong3());
  165. $location['country'] = $this->getstring();
  166. fseek($this->fp, $countryOffset + 4);
  167. $location['area'] = $this->getarea();
  168. break;
  169. default: // 否則,表示國家資訊沒有被重新導向
  170. $location['country'] = $this->getstring($byte);
  171. $location['area'] = $this->getarea();
  172. break;
  173. }
  174. break;
  175. case 2: // 標誌位元組為2,表示國家資訊被重新導向
  176. fseek($this->fp, $this->getlong3());
  177. $location['country'] = $this->getstring();
  178. fseek($this->fp, $offset + 8);
  179. $location['area'] = $this->getarea();
  180. break;
  181. default: // 否則,表示國家資訊沒有被重新導向
  182. $location['country'] = $this->getstring($byte);
  183. $location['area'] = $this->getarea();
  184. break;
  185. }
  186. if ($location['country'] == " CZ88.NET") { // CZ88.NET表示沒有有效資訊
  187. $location['country'] = "未知";
  188. }
  189. if ($location['area'] == " CZ88.NET") {
  190. $location['area'] = "";
  191. }
  192. return $location;
  193. }
  194. /**
  195. * 解構函式,用於在頁面執行結束後自動關閉開啟的檔案。
  196. *
  197. */
  198. function __desctruct() {
  199. if ($this->fp) {
  200. fclose($this->fp);
  201. }
  202. $this->fp = 0;
  203. }
  204. }
  205. ?>
複製代碼
  1. require_once('IpLocation.class.php');
  2. $ip='127.0.0.1';
  3. $idADDR=new IpLocation();
  4. print_r($idADDR->getlocation($ip));
  5. ?>
複製代碼
  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. /**
  25. * IP記錄的總條數(不包含版本資訊記錄)
  26. * @var int
  27. */
  28. var $totalip;
  29. /**
  30. * 建構函式,開啟 QQWry.Dat 檔案並初始化類中的資訊
  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. //註冊解構函式,使其在程式執行結束時執行
  41. register_shutdown_function(array(&$this, '__construct'));
  42. }
  43. }
  44. /**
  45. * 返回讀取的長整型數
  46. * @access private
  47. * @return int
  48. */
  49. function getlong() {
  50. //將讀取的little-endian編碼的4個位元組轉化為長整型數
  51. $result = unpack('Vlong', fread($this->fp, 4));
  52. return $result['long'];
  53. }
  54. /**
  55. * 返回讀取的3個位元組的長整型數
  56. *
  57. * @access private
  58. * @return int
  59. */
  60. function getlong3() {
  61. //將讀取的little-endian編碼的3個位元組轉化為長整型數
  62. $result = unpack('Vlong', fread($this->fp, 3).chr(0));
  63. return $result['long'];
  64. }
  65. /**
  66. * 返回壓縮後可進行比較的IP地址
  67. *
  68. * @access private
  69. * @param string $ip
  70. * @return string
  71. */
  72. function packip($ip) {
  73. // 將IP地址轉化為長整型數,如果在PHP5中,IP地址錯誤,則返回False,
  74. // 這時intval將Flase轉化為整數-1,之後壓縮成big-endian編碼的字串
  75. return pack('N', intval(ip2long($ip)));
  76. }
  77. /**
  78. * 返回讀取的字串
  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) { // 字串按照C格式儲存,以\0結束
  87. $data .= $char; // 將讀取的字元串連到給定字串之後
  88. $char = fread($this->fp, 1);
  89. }
  90. return $data;
  91. }
  92. /**
  93. * 返回地區資訊
  94. *
  95. * @access private
  96. * @return string
  97. */
  98. function getarea() {
  99. $byte = fread($this->fp, 1); // 標誌位元組
  100. switch (ord($byte)) {
  101. case 0: // 沒有地區資訊
  102. $area = "";
  103. break;
  104. case 1:
  105. case 2: // 標誌位元組為1或2,表示地區資訊被重新導向
  106. fseek($this->fp, $this->getlong3());
  107. $area = $this->getstring();
  108. break;
  109. default: // 否則,表示地區資訊沒有被重新導向
  110. $area = $this->getstring($byte);
  111. break;
  112. }
  113. return $area;
  114. }
  115. /**
  116. * 根據所給 IP 位址或網域名稱返回所在地區資訊
  117. * @access public
  118. * @param string $ip
  119. * @return array
  120. */
  121. function getlocation($ip) {
  122. if (!$this->fp) return null; // 如果資料檔案沒有被正確開啟,則直接返回空
  123. $location['ip'] = gethostbyname($ip); // 將輸入的網域名稱轉化為IP地址
  124. $ip = $this->packip($location['ip']); // 將輸入的IP地址轉化為可比較的IP地址
  125. // 不合法的IP地址會被轉化為255.255.255.255
  126. // 對分搜尋
  127. $l = 0; // 搜尋的下邊界
  128. $u = $this->totalip; // 搜尋的上邊界
  129. $findip = $this->lastip; // 如果沒有找到就返回最後一條IP記錄(QQWry.Dat的版本資訊)
  130. while ($l <= $u) { // 當上邊界小於下邊界時,尋找失敗
  131. $i = floor(($l + $u) / 2); // 計算近似中間記錄
  132. fseek($this->fp, $this->firstip + $i * 7);
  133. $beginip = strrev(fread($this->fp, 4)); // 擷取中間記錄的開始IP地址
  134. // strrev函數在這裡的作用是將little-endian的壓縮IP地址轉化為big-endian的格式
  135. // 以便用於比較,後面相同。
  136. if ($ip < $beginip) { // 使用者的IP小於中間記錄的開始IP地址時
  137. $u = $i - 1; // 將搜尋的上邊界修改為中間記錄減一
  138. } else {
  139. fseek($this->fp, $this->getlong3());
  140. $endip = strrev(fread($this->fp, 4)); // 擷取中間記錄的結束IP地址
  141. if ($ip > $endip) { // 使用者的IP大於中間記錄的結束IP地址時
  142. $l = $i + 1; // 將搜尋的下邊界修改為中間記錄加一
  143. } else { // 使用者的IP在中間記錄的IP範圍內時
  144. $findip = $this->firstip + $i * 7;
  145. break; // 則表示找到結果,退出迴圈
  146. }
  147. }
  148. }
  149. //擷取尋找到的IP地理位置資訊
  150. fseek($this->fp, $findip);
  151. $location['beginip'] = long2ip($this->getlong()); // 使用者IP所在範圍的開始地址
  152. $offset = $this->getlong3();
  153. fseek($this->fp, $offset);
  154. $location['endip'] = long2ip($this->getlong()); // 使用者IP所在範圍的結束位址
  155. $byte = fread($this->fp, 1); // 標誌位元組
  156. switch (ord($byte)) {
  157. case 1: // 標誌位元組為1,表示國家和地區資訊都被同時重新導向
  158. $countryOffset = $this->getlong3(); // 重新導向地址
  159. fseek($this->fp, $countryOffset);
  160. $byte = fread($this->fp, 1); // 標誌位元組
  161. switch (ord($byte)) {
  162. case 2: // 標誌位元組為2,表示國家資訊又被重新導向
  163. fseek($this->fp, $this->getlong3());
  164. $location['country'] = $this->getstring();
  165. fseek($this->fp, $countryOffset + 4);
  166. $location['area'] = $this->getarea();
  167. break;
  168. default: // 否則,表示國家資訊沒有被重新導向
  169. $location['country'] = $this->getstring($byte);
  170. $location['area'] = $this->getarea();
  171. break;
  172. }
  173. break;
  174. case 2: // 標誌位元組為2,表示國家資訊被重新導向
  175. fseek($this->fp, $this->getlong3());
  176. $location['country'] = $this->getstring();
  177. fseek($this->fp, $offset + 8);
  178. $location['area'] = $this->getarea();
  179. break;
  180. default: // 否則,表示國家資訊沒有被重新導向
  181. $location['country'] = $this->getstring($byte);
  182. $location['area'] = $this->getarea();
  183. break;
  184. }
  185. if ($location['country'] == " CZ88.NET") { // CZ88.NET表示沒有有效資訊
  186. $location['country'] = "未知";
  187. }
  188. if ($location['area'] == " CZ88.NET") {
  189. $location['area'] = "";
  190. }
  191. return $location;
  192. }
  193. /**
  194. * 解構函式,用於在頁面執行結束後自動關閉開啟的檔案。
  195. *
  196. */
  197. function __desctruct() {
  198. if ($this->fp) {
  199. fclose($this->fp);
  200. }
  201. $this->fp = 0;
  202. }
  203. }
  204. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.