[导入]获得客户端ip地址的方法

public static string GetUserIP(System.Web.UI.Page m_p)
{
   string clientip="";
              
   if(m_p.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
   {
    clientip = m_p.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Trim();
   }
   if (clientip.Length ==0)
   {
    clientip = m_p.Request.ServerVariables["REMOTE_ADDR"];
   }
   if (clientip.IndexOf(",") > -1)
   {
    string[] tempIp = clientip.Split(',');
    clientip = tempIp[tempIp.Length - 1].Trim();
   }
   try
   {
    System.Net.IPAddress.Parse(clientip);
   }
   catch
   {
    clientip = "127.127.0.1";
   }

   return clientip;
}


文章来源:http://blog.csdn.net/shoutor/archive/2006/01/23/587032.aspx
posted on 2006-02-21 14:09  RIVERSPIRIT  阅读(187)  评论(0)    收藏  举报