欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

做了CDN获取用户真实IP的函数代码(PHP与Asp设置方式)

程序员文章站 2022-05-07 23:37:05
asp函数代码:复制代码 代码如下:function checkip(checkstring)'用正则判断ip是否合法dim re1set re1=new regexpre...

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'];
}