-
-
/**
- * 依IP位址取得城市名稱 純真IP資料庫
- * edit: bbs.it-home.org
- */
- function convertip($ip) {
- //IP資料檔路徑
- $dat_path = 'QQWry.Dat';
-
- //檢查IP位址
- if(!filter_var($ip, FILTER_VALIDATE_IP)) {
- return 'IP Address Error';
- }
- return 'IP Address Error';
- }
- }
- //開啟IP資料檔
- if(!$fd = @fopen($dat_path, 'rb')){
- return 'IP date file not exists or access denied';
- }
-
- //分解IP進行運算,得出整形數
- $ipNum = ip2long($ip);
-
- //取得IP資料索引開始與結束位置
- $DataBegin = fread($fd, 4);
- $DataEnd = fread($fd, 4);
- $ipbegin = implode('', unpack('L', $DataBegin));
- if($ ipbegin $ipend = implode('', unpack('L', $DataEnd));
- if($ipend $ipNum || $ip2num $Middle= intval(($EndNum + $BeginNum) / 2);
-
- //偏移指標到索引位置讀取4個位元組
- fseek($fd, $ipbegin + 7 * $Middle);
- $ipData1 = fread($fd, 4);
- if(strlen($ipData1) fclose($fd);
- return 'System Error';
- }
- //提取出來的資料轉換成長整形,如果資料是負數則加上2的32次方
- $ ip1num = implode('', unpack('L', $ipData1));
- if($ip1num $ipNum) {
- $EndNum = $Middle;
- continue;
- }
-
- //取完上一個索引後取下一個索引
- $DataSeek = fread($fd, 3);
- if(strlen($DataSeek) fclose($fd);
- return 'System Error';
- }
- $DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
- fseek($fd, $DataSeek);
- $ipData2 = fread ($fd, 4);
- if(strlen($ipData2) fclose($fd);
- return 'System Error';
- }
- $ip2num = implode ('', unpack('L', $ipData2));
- if($ip2num
- //找不到提示未知
- if($ip2num if($Middle == $BeginNum) {
- fclose($fd);
- return 'Unknown';
- }
- $BeginNum = $Middle;
- }
- }
-
- //下面的程式碼讀暈了,沒讀明白,有興趣的慢慢讀
- $ipFlag = fread($fd, 1);
- if($ipFlag == chr(1)) {
- $ipSeek = fread($fd, 3);
- if(strlen($ipSeek) fclose($fd );
- return 'System Error';
- }
- $ipSeek = implode('', unpack('L', $ipSeek.chr(0)));
- fseek($fd, $ipSeek.chr(0)));
- fseek($fd, $ipSeek);
- $ipFlag = fread($fd, 1);
- }
-
- if($ipFlag == chr(2)) {
- $AddrSeek = fread($fd , 3);
- if(strlen($AddrSeek) fclose($fd);
- return 'System Error';
- }
- $ipFlag = fread($fd , 1);
- if($ipFlag == chr(2)) {
- $AddrSeek2 = fread($fd, 3);
- if(strlen($AddrSeek2) fclose($fd);
- return 'System Error';
- }
- $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0))); fseek ($fd, $AddrSeek2);
- } else {
- fseek($fd, -1, SEEK_CUR);
- }
-
- while(($char = fread($fd, 1 )) != chr(0))
- $ipAddr2 .= $char;
-
- $AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
- fseek($fd, $AddrSeek);
-
- while(($char = fread($fd, 1)) != chr(0))
- $ipAddr1 .= $char;
- } else {
- fseek($fd, -1, SEEK_CUR);
- while(($char = fread($fd, 1)) != chr(0))
- $ipAddr1 .= $ char;
-
- $ipFlag = fread($fd, 1);
- if($ipFlag == chr(2)) {
- $AddrSeek2 = fread($fd, 3);
- if(strlen($AddrSeek2) fclose($fd);
- return 'System Error';
- }
- $AddrSeek2 = implode('', unpack('L' , $AddrSeek2.chr(0)));
- fseek($fd, $AddrSeek2);
- } else {
- fseek($fd, -1, SEEK_CUR);
- }
- } } } } } } } } } } while(($char = fread($fd, 1)) != chr(0)){ $ipAddr2 .= $char; } }
- fclose($fd);
-
- //最後做對應的替換操作後回傳結果
- if(preg_match('/http/i', $ipAddr2)) {
- $ipAddr2 = '';
- }
- $ipaddr = "$ipAddr1 $ipAddr2";
- $ipaddr = preg_replace('/CZ88.Net/is', '', $ipaddr);
- $ipaddrdr = preg_replace('/^s*/is', '', $ipaddr);
- $ipaddr = preg_replace('/s*$/is', '', $ipaddr);
- if(preg_match( '/http/i', $ipaddr) || $ipaddr == '') {
- $ipaddr = '未知';
- }
-
- return $ipaddr;
- }
- header("內容類型:text/html; charset=utf-8");
- $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
- if(isset($_GET['q'])) {
- $ip = $_GET['q'];
- if(filter_var("http://".$ip,FILTER_VALIDATE_URL,FILTER_FLAG_HOST_REQUIRED)){
- $ip = gethostbyname($ip); 🎜> }
- }
- echo "$ip,".mb_convert_encoding(convertip($ip),"utf-8","gb2312");
-
- ?>
複製程式碼
|