通常来说,大家获取用户IP地址常用的方法是:
1 string IpAddress = ""; 2 if((HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]!=null 3 && HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] !=String.Empty) ) 4 { 5 IpAddress=HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ; 6 } 7 else 8 { 9 HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 10 }
事实上,上面的代码只试用与用户只使用了1层代理,如果用户有2层,3层HTTP_X_FORWARDED_FOR 的值是:"本机真实IP,1层代理IP,2层代理IP,....." ,如果这个时候你的数据中保存IP字段的长度很小(15个字节),数据库就报错了。
实际应用中,因为使用多层透明代理的情况比较少,所以这种用户并不多。
获取用户真实IP的方法:
1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Web; 5 using System.Web.Security; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 using System.Web.UI.WebControls.WebParts; 9 using System.Web.UI.HtmlControls; 10 using System.Text.RegularExpressions; 11 namespace Common 12 { 13 /// <summary> 14 /// IPAddress 的摘要说明 15 /// </summary> 16 public class IPAddress : System.Web.UI.Page 17 { 18 public static Int64 toDenaryIp ( string ip ) 19 { 20 Int64 _Int64 = 0; 21 string _ip = ip; 22 if ( _ip.LastIndexOf ( "." ) > -1 ) 23 { 24 string[] _iparray = _ip.Split ( '.' ); 25 _Int64 = Int64.Parse ( _iparray.GetValue ( 0 ).ToString() ) * 256 * 256 * 256 + Int64.Parse ( _iparray.GetValue ( 1 ).ToString() ) * 256 * 256 + Int64.Parse ( _iparray.GetValue ( 2 ).ToString() ) * 256 + Int64.Parse ( _iparray.GetValue ( 3 ).ToString() ) - 1; 26 } 27 return _Int64; 28 } 29 /// <summary> 30 /// /ip十进制 31 /// </summary> 32 public static Int64 DenaryIp 33 { 34 get { 35 Int64 _Int64 = 0; 36 string _ip = IP; 37 if ( _ip.LastIndexOf ( "." ) > -1 ) 38 { 39 string[] _iparray= _ip.Split ( '.' ); 40 _Int64 = Int64.Parse ( _iparray.GetValue ( 0 ).ToString() ) * 256 * 256 * 256 + Int64.Parse ( _iparray.GetValue ( 1 ).ToString() ) * 256 * 256 + Int64.Parse ( _iparray.GetValue ( 2 ).ToString() ) * 256 + Int64.Parse ( _iparray.GetValue ( 3 ).ToString() )-1; 41 } 42 return _Int64; 43 } 44 } 45 public static string IP 46 { 47 get 48 { 49 string result = String.Empty; 50 result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 51 if ( result != null && result != String.Empty ) 52 { 53 //可能有代理 54 if ( result.IndexOf ( "." ) == -1 ) //没有"."肯定是非IPv4格式 55 result = null; 56 else 57 { 58 if ( result.IndexOf ( "," ) != -1 ) 59 { 60 //有",",估计多个代理。取第一个不是内网的IP。 61 result = result.Replace ( " ", "" ).Replace ( "", "" ); 62 string[] temparyip = result.Split ( ",;".ToCharArray() ); 63 for ( int i = 0; i < temparyip.Length; i++ ) 64 { 65 if ( IsIPAddress ( temparyip[i] ) 66 && temparyip[i].Substring ( 0, 3 ) != "10." 67 && temparyip[i].Substring ( 0, 7 ) != "192.168" 68 && temparyip[i].Substring ( 0, 7 ) != "172.16." ) 69 { 70 return temparyip[i]; //找到不是内网的地址 71 } 72 } 73 } 74 else if ( IsIPAddress ( result ) ) //代理即是IP格式 75 return result; 76 else 77 result = null; //代理中的内容 非IP,取IP 78 } 79 } 80 string IpAddress = ( HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null && HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != String.Empty ) HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 81 82 if ( null == result || result == String.Empty ) 83 result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 84 if ( result == null || result == String.Empty ) 85 result = HttpContext.Current.Request.UserHostAddress; 86 return result; 87 } 88 } 89 //是否ip格式 90 public static bool IsIPAddress ( string str1 ) 91 { 92 if ( str1 == null || str1 == string.Empty || str1.Length < 7 || str1.Length > 15 ) return false; 93 string regformat = @"^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}$"; 94 Regex regex = new Regex ( regformat, RegexOptions.IgnoreCase ); 95 return regex.IsMatch ( str1 ); 96 } 97 } 98 }