VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • ASP.net教程之获取用户真实ip

public static string GetRealIP()
{
string result = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
if(string.IsNullOrEmpty(result)){
result = System.Web.HttpContext.Current.Request.Headers["HTTP_X_FORWARDED_FOR"];
}
if(string.IsNullOrEmpty(result)){
result = System.Web.HttpContext.Current.Request.Headers["HTTP_VIA"];
}
if(string.IsNullOrEmpty(result)){
result = System.Web.HttpContext.Current.Request.Headers["REMOTE_ADDR"];
}
if(string.IsNullOrEmpty(result)||!IsIp(result)){
result = "127.0.0.1";
}
return result;
}

/// <summary>
///是否为ip
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static bool IsIp(string ip)
{
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}


相关教程