昨天在寫程序的時候,發現在用戶的時候記錄IP和地區信息也許以后用得上,去網上找了找,發現實現的方式有好多好多,因為我用的ThinkPHP,后來又去TP官網找了找,最后采用了下面這種方法。

1 <?php 2 // +---------------------------------------------------------------------- 3 // | ThinkPHP [ WE CAN DO IT JUST THINK IT ] 4 // +---------------------------------------------------------------------- 5 // | Copyright (c) 2009 http://thinkphp.cn All rights reserved. 6 // +---------------------------------------------------------------------- 7 // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) 8 // +---------------------------------------------------------------------- 9 // | Author: liu21st <liu21st@gmail.com> 10 // +---------------------------------------------------------------------- 11 12 /** 13 * IP 地理位置查詢類 修改自 CoolCode.CN 14 * 由於使用UTF8編碼 如果使用純真IP地址庫的話 需要對返回結果進行編碼轉換 15 * @author liu21st <liu21st@gmail.com> 16 */ 17 class IpLocation { 18 /** 19 * QQWry.Dat文件指針 20 * 21 * @var resource 22 */ 23 private $fp; 24 25 /** 26 * 第一條IP記錄的偏移地址 27 * 28 * @var int 29 */ 30 private $firstip; 31 32 /** 33 * 最后一條IP記錄的偏移地址 34 * 35 * @var int 36 */ 37 private $lastip; 38 39 /** 40 * IP記錄的總條數(不包含版本信息記錄) 41 * 42 * @var int 43 */ 44 private $totalip; 45 46 /** 47 * 構造函數,打開 QQWry.Dat 文件並初始化類中的信息 48 * 49 * @param string $filename 50 * @return IpLocation 51 */ 52 public function __construct($filename = "UTFWry.dat") { 53 $this->fp = 0; 54 if (($this->fp = fopen(dirname(__FILE__).'/'.$filename, 'rb')) !== false) { 55 $this->firstip = $this->getlong(); 56 $this->lastip = $this->getlong(); 57 $this->totalip = ($this->lastip - $this->firstip) / 7; 58 } 59 } 60 61 /** 62 * 返回讀取的長整型數 63 * 64 * @access private 65 * @return int 66 */ 67 private function getlong() { 68 //將讀取的little-endian編碼的4個字節轉化為長整型數 69 $result = unpack('Vlong', fread($this->fp, 4)); 70 return $result['long']; 71 } 72 73 /** 74 * 返回讀取的3個字節的長整型數 75 * 76 * @access private 77 * @return int 78 */ 79 private function getlong3() { 80 //將讀取的little-endian編碼的3個字節轉化為長整型數 81 $result = unpack('Vlong', fread($this->fp, 3).chr(0)); 82 return $result['long']; 83 } 84 85 /** 86 * 返回壓縮后可進行比較的IP地址 87 * 88 * @access private 89 * @param string $ip 90 * @return string 91 */ 92 private function packip($ip) { 93 // 將IP地址轉化為長整型數,如果在PHP5中,IP地址錯誤,則返回False, 94 // 這時intval將Flase轉化為整數-1,之后壓縮成big-endian編碼的字符串 95 return pack('N', intval(ip2long($ip))); 96 } 97 98 /** 99 * 返回讀取的字符串 100 * 101 * @access private 102 * @param string $data 103 * @return string 104 */ 105 private function getstring($data = "") { 106 $char = fread($this->fp, 1); 107 while (ord($char) > 0) { // 字符串按照C格式保存,以\0結束 108 $data .= $char; // 將讀取的字符連接到給定字符串之后 109 $char = fread($this->fp, 1); 110 } 111 return $data; 112 // return iconv('gb2312', 'utf-8', $data); //通用函數,不能在這轉 113 } 114 115 /** 116 * 返回地區信息 117 * 118 * @access private 119 * @return string 120 */ 121 private function getarea() { 122 $byte = fread($this->fp, 1); // 標志字節 123 switch (ord($byte)) { 124 case 0: // 沒有區域信息 125 $area = ""; 126 break; 127 case 1: 128 case 2: // 標志字節為1或2,表示區域信息被重定向 129 fseek($this->fp, $this->getlong3()); 130 $area = $this->getstring(); 131 break; 132 default: // 否則,表示區域信息沒有被重定向 133 $area = $this->getstring($byte); 134 break; 135 } 136 return $area; 137 // return iconv('gb2312', 'utf-8', $area); //在最后統一轉 138 } 139 140 /** 141 * 根據所給 IP 地址或域名返回所在地區信息 142 * 143 * @access public 144 * @param string $ip 145 * @return array 146 */ 147 public function getlocation($ip='') { 148 if (!$this->fp) return null; // 如果數據文件沒有被正確打開,則直接返回空 149 if(empty($ip)) $ip = get_client_ip(); 150 $location['ip'] = gethostbyname($ip); // 將輸入的域名轉化為IP地址 151 $ip = $this->packip($location['ip']); // 將輸入的IP地址轉化為可比較的IP地址 152 // 不合法的IP地址會被轉化為255.255.255.255 153 // 對分搜索 154 $l = 0; // 搜索的下邊界 155 $u = $this->totalip; // 搜索的上邊界 156 $findip = $this->lastip; // 如果沒有找到就返回最后一條IP記錄(QQWry.Dat的版本信息) 157 while ($l <= $u) { // 當上邊界小於下邊界時,查找失敗 158 $i = floor(($l + $u) / 2); // 計算近似中間記錄 159 fseek($this->fp, $this->firstip + $i * 7); 160 $beginip = strrev(fread($this->fp, 4)); // 獲取中間記錄的開始IP地址 161 // strrev函數在這里的作用是將little-endian的壓縮IP地址轉化為big-endian的格式 162 // 以便用於比較,后面相同。 163 if ($ip < $beginip) { // 用戶的IP小於中間記錄的開始IP地址時 164 $u = $i - 1; // 將搜索的上邊界修改為中間記錄減一 165 } 166 else { 167 fseek($this->fp, $this->getlong3()); 168 $endip = strrev(fread($this->fp, 4)); // 獲取中間記錄的結束IP地址 169 if ($ip > $endip) { // 用戶的IP大於中間記錄的結束IP地址時 170 $l = $i + 1; // 將搜索的下邊界修改為中間記錄加一 171 } 172 else { // 用戶的IP在中間記錄的IP范圍內時 173 $findip = $this->firstip + $i * 7; 174 break; // 則表示找到結果,退出循環 175 } 176 } 177 } 178 179 //獲取查找到的IP地理位置信息 180 fseek($this->fp, $findip); 181 $location['beginip'] = long2ip($this->getlong()); // 用戶IP所在范圍的開始地址 182 $offset = $this->getlong3(); 183 fseek($this->fp, $offset); 184 $location['endip'] = long2ip($this->getlong()); // 用戶IP所在范圍的結束地址 185 $byte = fread($this->fp, 1); // 標志字節 186 switch (ord($byte)) { 187 case 1: // 標志字節為1,表示國家和區域信息都被同時重定向 188 $countryOffset = $this->getlong3(); // 重定向地址 189 fseek($this->fp, $countryOffset); 190 $byte = fread($this->fp, 1); // 標志字節 191 switch (ord($byte)) { 192 case 2: // 標志字節為2,表示國家信息又被重定向 193 fseek($this->fp, $this->getlong3()); 194 $location['country'] = $this->getstring(); 195 fseek($this->fp, $countryOffset + 4); 196 $location['area'] = $this->getarea(); 197 break; 198 default: // 否則,表示國家信息沒有被重定向 199 $location['country'] = $this->getstring($byte); 200 $location['area'] = $this->getarea(); 201 break; 202 } 203 break; 204 case 2: // 標志字節為2,表示國家信息被重定向 205 fseek($this->fp, $this->getlong3()); 206 $location['country'] = $this->getstring(); 207 fseek($this->fp, $offset + 8); 208 $location['area'] = $this->getarea(); 209 break; 210 default: // 否則,表示國家信息沒有被重定向 211 $location['country'] = $this->getstring($byte); 212 $location['area'] = $this->getarea(); 213 break; 214 } 215 if (trim($location['country']) == 'CZ88.NET') { // CZ88.NET表示沒有有效信息 216 $location['country'] = '未知'; 217 } 218 if (trim($location['area']) == 'CZ88.NET') { 219 $location['area'] = ''; 220 } 221 222 $location['country']=iconv('gb2312', 'utf-8', $location['country']); 223 $location['area']=iconv('gb2312', 'utf-8', $location['area']); 224 225 return $location; 226 } 227 228 /** 229 * 析構函數,用於在頁面執行結束后自動關閉打開的文件。 230 * 231 */ 232 public function __destruct() { 233 if ($this->fp) { 234 fclose($this->fp); 235 } 236 $this->fp = 0; 237 } 238 239 }
這個是TP里帶的,從上面的信息上來看已經很舊了,不過感覺寫的還不錯,就沒改什么,只是在后面加上了
$location['country']=iconv('gb2312', 'utf-8', $location['country']); $location['area']=iconv('gb2312', 'utf-8', $location['area']);
因為TP的純真數據庫已經轉碼了,但是很舊,似乎還是2012年的,IP這東西,那么老的東西估計很不准,於是我去純真官網下了一個,但是純真本身是GB2312的,所以要加上上面這兩句轉碼一下。
然后在項目中導入了這個PHP類以后,這樣去用就可以。
$ip=get_client_ip(); $ipData=new IpLocation("QQWry20140125.dat"); $ipInfo=$ipData->getlocation($ip);
得到的結果是這樣的,測試IP:218.197.147.154。
array (size=5) 'ip' => string '218.197.147.154' (length=15) 'beginip' => string '218.197.146.1' (length=13) 'endip' => string '218.197.147.154' (length=15) 'country' => string '湖北省武漢大學' (length=21) 'area' => string '外語自主學習中心' (length=24)
最新版的純真數據庫去純真官網下就可以,里面的QQWry.dat就是,用的時候也可以像我那樣在后面標上版本,免得以后記不清。然后把.dat跟上面的php類放在一個目錄下就OK啦。