VB.net 2010 視頻教程 VB.net 2010 視頻教程 VB.net 2010 視頻教程
SQL Server 2008 視頻教程 c#入門經典教程 Visual Basic從門到精通視頻教程
  • ASP.net教程之獲取用戶真實ip

  • 2019-04-18 22:19 來源:未知

魔兽世界wow www.geyjm.icu 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?)$");
}

相關教程