JDP 發表於 2011-9-26 13:29:15

取得Client真實IP

method 1
'==讀取IP==
   Dim strIPAddr As String
   Dim id As Integer
   If Request.ServerVariables("HTTP_X_FORWARDED_FOR") = "" Or InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), "unknown") > 0 Then
   strIPAddr = Request.ServerVariables("REMOTE_ADDR")
   ElseIf InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ",") > 0 Then
   strIPAddr = Mid(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), 1, InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ",") - 1)
   ElseIf InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ";") > 0 Then
   strIPAddr = Mid(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), 1, InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ";") - 1)
   Else
   strIPAddr = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
   End If
   '==讀出ID最後一號==
   SqlDataSource5.DataSourceMode = SqlDataSourceMode.DataReader
   Dim args As New DataSourceSelectArguments
   Dim I_DR As IDataReader = CType(SqlDataSource5.Select(args), IDataReader)
   I_DR.Read()
   id = Val(I_DR.Item("log_id").ToString) + 1
   I_DR.Close()
   I_DR.Dispose()

'最後只要印出下面這行就可以看到IP了。
Mid(strIPAddr, 1, 30).Trim.ToStringmethod 2
// 獲得客戶IP,透視代理
public static string GetClientIP()
{
   string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
   if (null == result || (result != null && String.IsNullOrEmpty(result)))
   {
      result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
   }
   if (null == result || (result != null && String.IsNullOrEmpty(result)))
   {
      result = HttpContext.Current.Request.UserHostAddress;
   }
   return result.Split(new Char[] { ',' });
}
Reference: http://www.programmer-club.com.tw/showSameTitleN/aspdotnet/19193.html
頁: [1]
查看完整版本: 取得Client真實IP