Home  >  Article  >  Backend Development  >  Made the CDN function code to obtain the user’s real IP (PHP and Asp setting method)_PHP tutorial

Made the CDN function code to obtain the user’s real IP (PHP and Asp setting method)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:12:37842browse

asp function code:

Copy code The code is as follows:

function checkip(checkstring)' Use regular expression to determine whether the IP Legal
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()'Get the real IP function, first HTTP_CLIENT_IP then HTTP_X_FORWARDED_FOR then 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 forum takes the php code of the real IP, other similar, please refer to the slight modification

(discuz modify include/common.inc.php)
Use the following code:

Copy the code The code is as follows:

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);

}

Replace this code:

Copy code The code is as follows:

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326649.htmlTechArticleasp function code: Copy the code as follows: function checkip(checkstring)' Use regular rules to determine whether the IP is legal dim re1 set re1=new RegExp re1.pattern=”^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn