- 相關推薦
php怎樣根據ip地址查地區
很多網站里一打開他們的網頁,便顯示“江蘇無錫的客戶您好”等等,請問他們是怎樣根據ip查到精確到市的地址的呢?下面是小編整理的php根據ip地址查地區,希望對大家有用,更多消息請關注應屆畢業生網。
根據discuz里面的ip查詢改的
/**
* ip地址所屬地區計算
* 修改自 discuz
* 使用dicuz tinyipdata數據文件
* 將一些英文提示修改為漢字
* $is_simple true的話顯示到市 false顯示到網通電信等等
******/
function convertip($ip,$is_simple=true,$ipfile='include/data/ip.dat') {
$return = '';
if( !file_exists($ipfile) ) $ipfile = '../'.$ipfile;
if(preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ip)) {
$iparray = explode('.', $ip);
if($iparray[0] == 10 || $iparray[0] == 127 || ($iparray[0] == 192 && $iparray[1] == 168) || ($iparray[0] == 172 && ($iparray[1] >= 16 && $iparray[1] <= 31))) {
return '局域網';
} elseif($iparray[0] > 255 || $iparray[1] > 255 || $iparray[2] > 255 || $iparray[3] > 255) {
return 'ERR';
} elseif($is_simple) {
return change_simply_area(convertip_tiny($ip, $ipfile));
}
else {
return convertip_tiny($ip, $ipfile);
}
}
}
/**
* 從ip文件得到ip所屬地區
*
* 過濾掉了具體的位置(如 網通/電信/**網吧) 基本到市
***/
function convertip_tiny($ip, $ipdatafile) {
static $fp = NULL, $offset = array(), $index = NULL;
$ipdot = explode('.', $ip);
$ip = pack('N', ip2long($ip));
$ipdot[0] = (int)$ipdot[0];
$ipdot[1] = (int)$ipdot[1];
if($fp === NULL && $fp = @fopen($ipdatafile, 'rb')) {
$offset = unpack('Nlen', fread($fp, 4));
$index = fread($fp, $offset['len'] - 4);
} elseif($fp == FALSE) {
return '- Invalid IP data file';
}
$length = $offset['len'] - 1028;
$start = unpack('Vlen', $index[$ipdot[0] * 4] . $index[$ipdot[0] * 4 + 1] . $index[$ipdot[0] * 4 + 2] . $index[$ipdot[0] * 4 + 3]);
for ($start = $start['len'] * 8 + 1024; $start < $length; $start += 8) {
if ($index{$start} . $index{$start + 1} . $index{$start + 2} . $index{$start + 3} >= $ip) {
$index_offset = unpack('Vlen', $index{$start + 4} . $index{$start + 5} . $index{$start + 6} . "\x0");
$index_length = unpack('Clen', $index{$start + 7});
break;
}
}
fseek($fp, $offset['len'] + $index_offset['len'] - 1024);
if($index_length['len']) {
return mb_convert_encoding(fread($fp, $index_length['len']),'utf-8','gb2312'); //將讀出的gb編碼數據轉成utf-8并返回
} else {
return '未知';
}
}
function change_simply_area($area) {
$tmp = explode(' ',$area); //過濾掉一些具體信息
return $tmp[0];
}
里面那個ipfile你可以去下載一個discuz 在 ipdata目錄里面有wry.dat的文件就是了 其實這個就是網上用的最多的那個純真版數據庫 很多顯示ip的qq用的也是那個
這兩個函數的原型參考 discuz 里面 include\misc.func.php
【php怎樣根據ip地址查地區】相關文章:
php通過Chianz.com獲取IP地址與地區的方法08-12
php如何獲得ip地址09-06
php獲取IP物理地址的方法10-10
ip地址如何修改呢08-23
PHP獲取真實的客戶IP的方法09-01
IP地址計算和子網劃分06-21
電腦如何自動獲取ip地址06-02
PHP中用CURL偽造IP來源的方法06-26
查看路由器IP地址方法07-01
ip地址與網絡其他系統沖突怎么解決01-13