How to obtain the real IP after using CDN (127.0.0.1)

Published: 2010-02-25

```html

    When logging in as a member or logging in to backend management on virtual hosting, the user client address is generally obtained and recorded in the database. However, after the same program is deployed on our company's virtual hosting, the obtained client address is not the user's real IP address or is "127.0.0.1". You can solve this by modifying the code section for obtaining the client IP address in the following ways.


How to obtain the real IP after using CDN with Supesite

In the include/main.inc.php file, this part

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

Replace with

function get_real_ip()
{
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"]))
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip)
{
array_unshift($ips, $ip); $ip = FALSE;
}
for ($i = 0; $i < count($ips); $i++)
{
if(!preg_match("/^(10|172\.16|192\.168)\./", $ $ips[$i])){
$ip = $ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
$_SGLOBAL['onlineip'] = get_real_ip();

How to obtain the real IP after using CDN with Discuz!

nclude/common.inc.php
Find the following code:

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


Replace with:

function get_real_ip()
{
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"]))
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip)
{
array_unshift($ips, $ip); $ip = FALSE;
}
for ($i = 0; $i < count($ips); $i++)
{
if(!preg_match("/^(10|172\.16|192\.168)\./", $ $ips[$i]))

{
$ip = $ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
$onlineip = get_real_ip();



IP logs recorded in the 帝国CMS backend and IPs recorded for user registration

In \e\class\connect.php, search for the IP acquisition code in the function egetip() and modify it to the above part


Solution for DZ registered user IP being the same as the invite code owner's IP

Method A:


register.php line 200

showmessage('register_invite_iperror');
change to
//showmessage('register_invite_iperror');

Method B:
In the include/common.inc.php file, change

Code:

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

Replace with:

Code:
$onlineip=getenv('HTTP_X_FORWARD_FOR');

--------------------

How to get the user's real IP program for an ASP website that uses CDN


function   checkip(checkstring)'Use a regular expression to check whether the IP is valid
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()'Function to get the real IP; 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

6 ways to obtain IP in ASP.NET

Server side:

//Method 1
HttpContext.Current.Request.UserHostAddress; 

//Method 2
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

//Method 3
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

//Method 4 (ignores proxy)
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

Client side:

//Method 5
var ip = '<!--#echo var="REMOTE_ADDR"-->';
alert(
"Your IP address is "+ip);

//Method 6 (ignores proxy)
function GetLocalIPAddress() 
{ 
    
var obj = null; 
    
var rslt = ""; 
    
try 
    
{ 
        obj 
= new ActiveXObject("rcbdyctl.Setting"); 
        rslt 
= obj.GetIPAddress; 
        obj 
= null; 
    }
 
    
catch(e) 
    
{ 
        
// 
    }
 
     
    
return rslt; 
   }
 

MCT Maulik Patel from India provided a server-side solution, which is very good:

if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // using proxy
{ 
     ip
=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();  // Return real client IP.
}

else// not using proxy or can't get the Client IP
{ 
     ip
=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
}


Notes:

1. Some proxies will not send us the real IP address.

2. Some clients will not send us the IP because of the security setting "header_access deny".
 

For .NET, you can also refer to http://www.cdnunion.com/articleinfo.php?pid=158 for settings.

```