获取用户真实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?)$");
}
上一篇: 多线程系列之自己实现一个 lock 锁
下一篇: JS选取DOM元素常见操作方法实例分析