public static class IpHelper
{
/// <summary>
/// 获取Ip
/// </summary>
/// <returns></returns>
public static string GetIp()
{
string[] IP_Ary;
string strIP, strIP_list;
strIP_list = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (strIP_list != null && strIP_list != "")
{
strIP_list = strIP_list.Replace("'", "");
if (strIP_list.IndexOf(",") >= 0)
{
IP_Ary = strIP_list.Split(',');
strIP = IP_Ary[0];
}
else
{
strIP = strIP_list;
}
}
else
{
strIP = "";
}
if (strIP == "")
{
strIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
strIP = strIP.Replace("'", "");
}
return strIP;
}
private static double IpToNum(string ip)
{
int s1 = 1;
int s2 = 256 * s1;
int s3 = 256 * s2;
int s4 = 256 * s3;
string[] IpNs = ip.Trim().Split('.');
try
{
double IpN = Convert.ToDouble(IpNs[0]) * s4 + Convert.ToDouble(IpNs[1]) * s3 + IpNs[2].ToInt() * s2 + IpNs[3].ToInt() * s1;
return IpN;
}
catch (Exception)
{
return 0;
}
}
}