/// <summary>
/// 获得当前页面客户端的IP
/// </summary>
/// <returns>当前页面客户端的IP</returns>
public static string GetIP()
{
string result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(result))
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(result))
result = HttpContext.Current.Request.UserHostAddress; if (string.IsNullOrEmpty(result) || !Utils.IsIP(result))
return "127.0.0.1"; return result;
}
/// 获得当前页面客户端的IP
/// </summary>
/// <returns>当前页面客户端的IP</returns>
public static string GetIP()
{
string result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(result))
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(result))
result = HttpContext.Current.Request.UserHostAddress; if (string.IsNullOrEmpty(result) || !Utils.IsIP(result))
return "127.0.0.1"; return result;
}
用["REMOTE_ADDR"]和["HTTP_X_FORWARDED_FOR"]来取
取的是服务器的IP
有没有别的办法
HttpContext.Current.Request.UserHostAddress
如果是在家里用无线路由器上网,IP地址是192.168.x.x,但实际上是用的外网IP.
在服务端也是,如果服务器放在路由器后面,得到的客户端IP就只是自己的路由器IP地址.
//Label20.Text = Request.UserHostAddress;
//string IP = Request.UserHostAddress;
//string sql = "insert into 用户表(用户账号) values ('" + IP.Trim() + "')";
//DA.ExeSQL(sql);
//string sa = "";
//sa = "delete from 用户表 where 用户账号 = '" + IP.Trim() + "'";
//DA.ExeSQL(sa);
Label20.Text = Request.UserHostAddress;
string IP = Request.UserHostAddress;
string sql = "insert into 用户表(用户账号) values ('" + IP.Trim() + "')";
DA.ExeSQL(sql);
string sa = "";
sa = "delete from 用户表 where 用户账号 = '" + IP.Trim() + "'";
DA.ExeSQL(sa);