PHP 通過(guò)IP獲取當(dāng)前城市
PHP獲取IP地址
這個(gè)比較簡(jiǎn)單了,利用PHP自帶函數(shù)就可以了,PHP中文手冊(cè)看一下,都有現(xiàn)成的例子,就不過(guò)多說(shuō)明了,直接上代碼,
A段:
$xp_UserIp = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
$xp_UserIp = ($xp_UserIp) ? $xp_UserIp : $_SERVER["REMOTE_ADDR"]; ?>
PHP通過(guò)IP地址判斷用戶所在城市
上文已經(jīng)獲得了用戶IP地址,接下來(lái),我們就是根據(jù)這個(gè)IP地址獲得用戶所在城市了。開(kāi)始之前,我們需要下載一個(gè)現(xiàn)成的數(shù)據(jù)庫(kù)QQ IP數(shù)據(jù)庫(kù)。
附:最新騰訊QQ IP數(shù)據(jù)庫(kù) 2011.04.10 純真版下載
使用方法:解壓后QQWry.Dat就是我們想要IP地址數(shù)據(jù)庫(kù),我們新建一個(gè)ipcity文件夾,將數(shù)據(jù)庫(kù)放在下面。QQ IP數(shù)據(jù)庫(kù)使用非常方便,數(shù)據(jù)也很齊全,你可以及時(shí)關(guān)注官方更新以保持?jǐn)?shù)據(jù)最新,強(qiáng)力推薦一下:)
接下來(lái),我們?cè)谏厦娴膇pcity目錄下新建一個(gè)ipaddress.php文件,直接復(fù)制以下代碼進(jìn)去即可,重要的地方也作了相應(yīng)注釋。
B段:
/* 函數(shù)名稱:ipCity
參數(shù)說(shuō)明:$userip——用戶IP地址
函數(shù)功能:PHP通過(guò)IP地址判斷用戶所在城市
author:jason
contact:admin@sslook.com */
function ipCity($userip) {
//IP數(shù)據(jù)庫(kù)路徑,這里用的是QQ IP數(shù)據(jù)庫(kù) 20110405 純真版
$dat_path = ’QQWry.dat’;
//判斷IP地址是否有效
if(!ereg(“^([0-9]{1,3}.){3}[0-9]{1,3}$”, $userip)){
return ’IP Address Invalid’;
}
//打開(kāi)IP數(shù)據(jù)庫(kù)
if(!$fd = @fopen($dat_path, ’rb’)){
return ’IP data file not exists or access denied’;
}
//explode函數(shù)分解IP地址,運(yùn)算得出整數(shù)形結(jié)果
$userip = explode(‘.’, $userip);
$useripNum = $userip[0] * 16777216 + $userip[1] * 65536 + $userip[2] * 256 + $userip[3];
//獲取IP地址索引開(kāi)始和結(jié)束位置
$DataBegin = fread($fd, 4);
$DataEnd = fread($fd, 4);
$useripbegin = implode(”, unpack(‘L’, $DataBegin));
if($useripbegin < 0) $useripbegin += pow(2, 32);
$useripend = implode(”, unpack(‘L’, $DataEnd));
if($useripend < 0) $useripend += pow(2, 32);
$useripAllNum = ($useripend - $useripbegin) / 7 + 1;
$BeginNum = 0;
$EndNum = $useripAllNum;
//使用二分查找法從索引記錄中搜索匹配的IP地址記錄
while($userip1num>$useripNum || $userip2num<$useripNum) {
$Middle= intval(($EndNum + $BeginNum) / 2);
//偏移指針到索引位置讀取4個(gè)字節(jié)
fseek($fd, $useripbegin + 7 * $Middle);
$useripData1 = fread($fd, 4);
if(strlen($useripData1) < 4) {
fclose($fd);
return ’File Error’;
}
//提取出來(lái)的數(shù)據(jù)轉(zhuǎn)換成長(zhǎng)整形,如果數(shù)據(jù)是負(fù)數(shù)則加上2的32次冪
$userip1num = implode(”, unpack(‘L’, $useripData1));
if($userip1num < 0) $userip1num += pow(2, 32);
//提取的長(zhǎng)整型數(shù)大于我們IP地址則修改結(jié)束位置進(jìn)行下一次循環(huán)
if($userip1num > $useripNum) {
$EndNum = $Middle;
continue;
}
//取完上一個(gè)索引后取下一個(gè)索引
$DataSeek = fread($fd, 3);
if(strlen($DataSeek) < 3) {
fclose($fd);
return ’File Error’;
}
$DataSeek = implode(”, unpack(‘L’, $DataSeek.chr(0)));
fseek($fd, $DataSeek);
$useripData2 = fread($fd, 4);
if(strlen($useripData2) < 4) {
fclose($fd);
return ’File Error’;
}
$userip2num = implode(”, unpack(‘L’, $useripData2));
if($userip2num < 0) $userip2num += pow(2, 32);
//找不到IP地址對(duì)應(yīng)城市
if($userip2num < $useripNum) {
if($Middle == $BeginNum) {
fclose($fd);
return ’No Data’;
}
$BeginNum = $Middle;
}
}
$useripFlag = fread($fd, 1);
if($useripFlag == chr(1)) {
$useripSeek = fread($fd, 3);
if(strlen($useripSeek) < 3) {
fclose($fd);
return ’System Error’;
}
$useripSeek = implode(”, unpack(‘L’, $useripSeek.chr(0)));
fseek($fd, $useripSeek);
$useripFlag = fread($fd, 1);
}
if($useripFlag == chr(2)) {
$AddrSeek = fread($fd, 3);
if(strlen($AddrSeek) < 3) {
fclose($fd);
return ’System Error’;
}
$useripFlag = fread($fd, 1);
if($useripFlag == chr(2)) {
$AddrSeek2 = fread($fd, 3);
if(strlen($AddrSeek2) < 3) {
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))
$useripAddr2 .= $char;
$AddrSeek = implode(”, unpack(‘L’, $AddrSeek.chr(0)));
fseek($fd, $AddrSeek);
while(($char = fread($fd, 1)) != chr(0))
$useripAddr1 .= $char; } else {
fseek($fd, -1, SEEK_CUR);
while(($char = fread($fd, 1)) != chr(0))
$useripAddr1 .= $char;
$useripFlag = fread($fd, 1);
if($useripFlag == chr(2)) {
$AddrSeek2 = fread($fd, 3);
if(strlen($AddrSeek2) < 3) {
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)){
$useripAddr2 .= $char;
} }
fclose($fd);
//返回IP地址對(duì)應(yīng)的城市結(jié)果
if(preg_match(‘/http/i’, $useripAddr2)) {
$useripAddr2 = ”;
}
$useripaddr = ”$useripAddr1 $useripAddr2″;
$useripaddr = preg_replace(‘/CZ88.Net/is’, ”, $useripaddr);
$useripaddr = preg_replace(‘/^s*/is’, ”, $useripaddr);
$useripaddr = preg_replace(‘/s*$/is’, ”, $useripaddr);
if(preg_match(‘/http/i’, $useripaddr) || $useripaddr == ”) {
$useripaddr = ’No Data’;
}
return $useripaddr;
}
?>
PHP根據(jù)IP地址實(shí)現(xiàn)城市切換或跳轉(zhuǎn)
到這里,其實(shí)問(wèn)題已經(jīng)很簡(jiǎn)單了,用簡(jiǎn)單的js就通通搞定。
C段如下:
//根據(jù)IP地址跳轉(zhuǎn)指定頁(yè)面js取得城市
var city=’’;
//根據(jù)IP地址所有城市跳轉(zhuǎn)到指定頁(yè)面
if(city.indexOf(“上海市”)>=0){
window.location.href=”http://shanghai.demo.com/”;
}
將開(kāi)頭的A段代碼和上面的C段代碼分別放在B段代碼的頭和尾,然后我們?cè)谛枰D(zhuǎn)的頁(yè)面加入以下代碼:
刷新頁(yè)面,是不是達(dá)到預(yù)想的效果了呢?
推薦文章
2024-11-28
2024-06-25
2024-01-04
2023-11-06
2023-10-30
2023-10-13
2023-10-10
穩(wěn)定
產(chǎn)品高可用性高并發(fā)貼心
項(xiàng)目群及時(shí)溝通專業(yè)
產(chǎn)品經(jīng)理1v1支持快速
MVP模式小步快跑承諾
我們選擇聲譽(yù)堅(jiān)持
10年專注高端品質(zhì)開(kāi)發(fā)聯(lián)系我們
友情鏈接: