程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> 做了CDN獲取用戶真實IP的函數代碼(PHP與Asp設置方式)

做了CDN獲取用戶真實IP的函數代碼(PHP與Asp設置方式)

編輯:PHP綜合

asp函數代碼:
復制代碼 代碼如下:
function checkip(checkstring)'用正則判斷IP是否合法
dim re1
set re1=new RegExp
re1.pattern=”^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$”
re1.global=false
re1.Ignorecase=false
checkip=re1.test(checkstring)
set re1=nothing
end function

function get_cli_ip()'取真實IP函數,先 HTTP_CLIENT_IP 再 HTTP_X_FORWARDED_FOR 再 REMOTE_ADDR
dim client_ip
if checkip(Request.ServerVariables(”HTTP_CLIENT_IP”))=true then
get_cli_ip = checkip(Request.ServerVariables(”HTTP_CLIENT_IP”))
else
MyArray = split(Request.ServerVariables(”HTTP_X_FORWARDED_FOR”),”,”)
if ubound(MyArray)>=0 then
client_ip = trim(MyArray(0))
if checkip(client_ip)=true then
get_cli_ip = client_ip
exit function
end if
end if
get_cli_ip = Request.ServerVariables(”REMOTE_ADDR”)
end if
end function

discuz論壇取真實IP的php代碼,其它類似,請參考稍做修改

(discuz修改include/common.inc.php)
用以下這段代碼:
復制代碼 代碼如下:
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'),'unknown')) {
$onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),

'unknown')) {

$testip = explode('.', getenv('HTTP_X_FORWARDED_FOR'));

if ($testip[0]=='192′ && $testip[1]=='168′) {
$onlineip = getenv('REMOTE_ADDR');
}
elseif($testip[0]=='10′) {
$onlineip = getenv('REMOTE_ADDR');
}
else {
$onlineip = getenv('HTTP_X_FORWARDED_FOR');
}

//gamesir hack end} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'),

'unknown')) {
$onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER

['REMOTE_ADDR'],'unknown')) {
//by Johnny
$tmp_ip = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
$tmp_ip1 = explode(',',$tmp_ip[0]);
if ($tmp_ip1[0] =='192′ && $tmp_ip1[1] =='168′) {
$onlineip = getenv('REMOTE_ADDR');
}else if($tmp_ip1[0]=='10′) {
$onlineip = getenv('REMOTE_ADDR');
}
else{
$onlineip = $tmp_ip[0];
}
unset($tmp_ip);unset($tmp_ip1);

}

替換這段代碼:

復制代碼 代碼如下:
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'),'unknown')) {
$onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),

'unknown')) {
$onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'),'unknown')) {
$onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER

['REMOTE_ADDR'],'unknown')) {
$onlineip = $_SERVER['REMOTE_ADDR'];
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved