PHP利用純真IP資料庫在本地實現IP位址資訊查詢

2021-09-26 10:04:32 字數 2490 閱讀 9103

準備工作:

正面這個ip位址查詢處理的類

<?php  

/**

* ip 地理位置查詢類

*/

class helper_iplocation

} /**

* 析構函式,用於在頁面執行結束後自動關閉開啟的檔案。

* */

public function __destruct()

$this->fp = 0;

} /**

* 返回讀取的長整型數

* * @access private

* @return int

*/

private function getlong()

/**

* 返回讀取的3個位元組的長整型數

* * @access private

* @return int

*/

private function getlong3()

/**

* 返回壓縮後可進行比較的ip位址

* * @access private

* @param string $ip

* @return string

*/

private function packip($ip)

/**

* 返回讀取的字串

* * @access private

* @param string $data

* @return string

*/

private function getstring($data = "")

return iconv('gbk', 'utf-8', $data);

} /**

* 返回地區資訊

* * @access private

* @return string

*/

private function getarea()

return $area;

} /**

* 根據所給 ip 位址或網域名稱返回所在地區資訊

* * @access public

* @param string $ip

* @return array

*/

public function getlocation($ip)

else else

} }

//獲取查詢到的ip地理位置資訊

fseek($this->fp, $findip);

$location['beginip'] = long2ip($this->getlong()); // 使用者ip所在範圍的開始位址

$offset = $this->getlong3();

fseek($this->fp, $offset);

$location['endip'] = long2ip($this->getlong()); // 使用者ip所在範圍的結束位址

$byte = fread($this->fp, 1); // 標誌位元組

switch (ord($byte))

break;

case 2: // 標誌位元組為2,表示國家資訊被重定向

fseek($this->fp, $this->getlong3());

$location['country'] = $this->getstring();

fseek($this->fp, $offset + 8);

$location['area'] = $this->getarea();

break;

default: // 否則,表示國家資訊沒有被重定向

$location['country'] = $this->getstring($byte);

$location['area'] = $this->getarea();

break;

} if ($location['country'] == " cz88.net")

if ($location['area'] == " cz88.net")

return $location;

} }

呼叫方式:

<?php  

include 'iplocation.php';

echo "";

$ip = "180.76.6.130";

$iplocation = new iplocation();

$location = $iplocation->getlocation($ip);

print_r($location);

PHP利用純真IP資料庫在本地實現IP位址資訊查詢

準備工作 正面這個ip位址查詢處理的類 ip 地理位置查詢類 class helper iplocation 析構函式,用於在頁面執行結束後自動關閉開啟的檔案。public function destruct this fp 0 返回讀取的長整型數 access private return int...

純真IP庫PHP查詢

class ip 檢查ip的合法性 public function checkip ip return true 讀取little endian編碼的4個位元組轉化為長整型數 public function getlong4 讀取little endian編碼的3個位元組轉化為長整型數 public...

無聊到轉純真IP資料庫為MDB

那天 想在網上做個位址 統計,找了半天發現沒有現成的,就用純真資料庫來轉,首先解成txt的文字有20m 然後以空格分割匯入excel 首先是ip 轉數字,然後是 地區部分由於有空格被分成了n多個字段。自帶函式沒有辦法,所以就開始寫巨集 如下 當然這個已經是改了幾次了,這個是可以正常執行的了,但是36...