顶部信息栏,显示网站标题、Logo等资

How to Get Client IP Address

11/18/2014 Source:Backyard [comment]


In ASP, if you can use Request.ServerVariables("REMOTE_ADDR")  to get client IP address,but if the client use proxy server to visit you web,you just can get that proxy server's IP address,not the real client IP address.
 
If you want to get the client IP address by proxy server, you can use:

 string ipaddress= Request.ServerVariables("HTTP_X_FORWARDED_FOR") ;
 
but you need to know, not all the proxy server can use Request.ServerVariables("HTTP_X_FORWARDED_FOR") to get the client IP address , some of them  is still the server IP address.
 
more you need to know: if client do not use proxy server to visit you web, then the  Request.ServerVariables ("HTTP_X_FORWARDED_FOR") will get null, so, if you use this method in you code you should use it like this:
......
userip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If userip = "" Then userip = Request.ServerVariables("REMOTE_ADDR")
......
  
code 1
HttpContext.Current.Request.UserHostAddress;
  
 code 2
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  
code3
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
  
code4
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
 
client side:
  
code 5
var ip = '<!--#echo var="REMOTE_ADDR"-->';
alert("Your IP address is "+ip);
 
code 6
function GetLocalIPAddress()
  {
 
var obj = null;
 
var rslt = "";
 
try
 
{

obj = new ActiveXObject("rcbdyctl.Setting");

rslt = obj.GetIPAddress;

obj = null;
 
}
 
catch(e)
 
{

//
 
}

return rslt;
  }
  

another one come from Intenet:
 
if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // server, using proxy
{
  
    get the real client IP address,
  
ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP.
}
else//if not using proxy or can't get the Client IP
{

get server IP address,
  
ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
}
some client may not sent us IP address for “header_access deny” security setting.