找回密碼
 註冊
搜索
查看: 4556|回復: 0

取得Client真實IP

[複製鏈接]
發表於 2011-9-26 13:29:15 | 顯示全部樓層 |閱讀模式
method 1
  1. '==讀取IP==
  2.      Dim strIPAddr As String
  3.      Dim id As Integer
  4.      If Request.ServerVariables("HTTP_X_FORWARDED_FOR") = "" Or InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), "unknown") > 0 Then
  5.      strIPAddr = Request.ServerVariables("REMOTE_ADDR")
  6.      ElseIf InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ",") > 0 Then
  7.      strIPAddr = Mid(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), 1, InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ",") - 1)
  8.      ElseIf InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ";") > 0 Then
  9.      strIPAddr = Mid(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), 1, InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ";") - 1)
  10.      Else
  11.      strIPAddr = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
  12.      End If
  13.      '==讀出ID最後一號==
  14.      SqlDataSource5.DataSourceMode = SqlDataSourceMode.DataReader
  15.      Dim args As New DataSourceSelectArguments
  16.      Dim I_DR As IDataReader = CType(SqlDataSource5.Select(args), IDataReader)
  17.      I_DR.Read()
  18.      id = Val(I_DR.Item("log_id").ToString) + 1
  19.      I_DR.Close()
  20.      I_DR.Dispose()

  21. '最後只要印出下面這行就可以看到IP了。
  22. Mid(strIPAddr, 1, 30).Trim.ToString
複製代碼
method 2
  1. // 獲得客戶IP,透視代理
  2. public static string GetClientIP()
  3. {
  4.    string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  5.    if (null == result || (result != null && String.IsNullOrEmpty(result)))
  6.    {
  7.       result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  8.    }
  9.    if (null == result || (result != null && String.IsNullOrEmpty(result)))
  10.    {
  11.       result = HttpContext.Current.Request.UserHostAddress;
  12.    }
  13.    return result.Split(new Char[] { ',' })[0];
  14. }
複製代碼

Reference: http://www.programmer-club.com.tw/showSameTitleN/aspdotnet/19193.html
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

Archiver|手機版|彩色筆小沛的知識庫

GMT+8, 2024-12-22 10:24 , Processed in 0.019440 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回復 返回頂部 返回列表