asp讀取IP庫(dat檔案)

來源:互聯網
上載者:User

<%
' ============================================
' 返回IP資訊
' ============================================
Function Look_Ip(IP)
 Dim Wry, IPType, QQWryVersion, IpCounter
 ' 設定類對象
 Set Wry = New TQQWry
 ' 開始搜尋,並返回搜尋結果
 ' 您可以根據 QQWry(IP) 傳回值來判斷該IP地址在資料庫中是否存在,如果不存在可以執行其他的一些操作
 ' 比如您自建一個資料庫作為追捕等,這裡我就不詳細說明了
 IPType = Wry.QQWry(IP)
 ' Country:國家地區欄位
 ' LocalStr:省市及其他資訊欄位
 Look_Ip = Wry.Country & " " & Wry.LocalStr
End Function
' ============================================
' 返回IP資訊 JS調用
' ============================================
Function GetIpInfoAv(IP, sType)
 Dim Wry, IPType
 Set Wry = New TQQWry
 IPType = Wry.QQWry(IP)
 
 Select Case sType
  Case 1 GetIpInfoAv = "document.write(""" & IP & """);"
  Case 2 GetIpInfoAv = "document.write(""" & Wry.Country & """);"
  Case 3 GetIpInfoAv = "document.write(""" & Wry.LocalStr & """);"
  Case Else GetIpInfoAv = "document.write(""您來自:" & IP & " 所在地區:" & Wry.Country & " " & Wry.LocalStr & """);"
 End Select
End Function
' ============================================
' 返回QQWry資訊(QQWry.Dat版本以及記錄條數)
' ============================================
Function WryInfo()
 Dim Wry, IPType, QQWry(1)
 ' 設定類對象
 Set Wry = New TQQWry
 IPType = Wry.QQWry("255.255.255.255")
 ' 讀取資料庫版本資訊
 QQWry(0) = Wry.Country & " " & Wry.LocalStr
 ' 讀取資料庫IP地址數目
 QQWry(1) = Wry.RecordCount + 1
 WryInfo = QQWry
End Function
' ============================================
' IP物理定位搜尋類
' ============================================
Class TQQWry
 ' ============================================
 ' 變數聲名
 ' ============================================
 Dim Country, LocalStr, Buf, OffSet
 Private StartIP, EndIP, CountryFlag
 Public QQWryFile
 Public FirstStartIP, LastStartIP, RecordCount
 Private Stream, EndIPOff
 ' ============================================
 ' 類別模組初始化
 ' ============================================
 Private Sub Class_Initialize
  Country   = ""
  LocalStr   = ""
  StartIP   = 0
  EndIP    = 0
  CountryFlag  = 0
  FirstStartIP  = 0
  LastStartIP  = 0
  EndIPOff   = 0
  QQWryFile = Server.MapPath("CoralWry.dat") 'QQ IP庫路徑,要轉換成實體路徑
 End Sub
 ' ============================================
 ' IP地址轉換成整數
 ' ============================================
 Function IPToInt(IP)
  Dim IPArray, i
  IPArray = Split(IP, ".", -1)
  FOr i = 0 to 3
   If Not IsNumeric(IPArray(i)) Then IPArray(i) = 0
   If CInt(IPArray(i)) < 0 Then IPArray(i) = Abs(CInt(IPArray(i)))
   If CInt(IPArray(i)) > 255 Then IPArray(i) = 255
  Next
  IPToInt = (CInt(IPArray(0))*256*256*256) + (CInt(IPArray(1))*256*256) + (CInt(IPArray(2))*256) + CInt(IPArray(3))
 End Function
 ' ============================================
 ' 整數逆轉IP地址
 ' ============================================
 Function IntToIP(IntValue)
  p4 = IntValue - Fix(IntValue/256)*256
  IntValue = (IntValue-p4)/256
  p3 = IntValue - Fix(IntValue/256)*256
  IntValue = (IntValue-p3)/256
  p2 = IntValue - Fix(IntValue/256)*256
  IntValue = (IntValue - p2)/256
  p1 = IntValue
  IntToIP = Cstr(p1) & "." & Cstr(p2) & "." & Cstr(p3) & "." & Cstr(p4)
 End Function
 ' ============================================
 ' 擷取開始IP位置
 ' ============================================
 Private Function GetStartIP(RecNo)
  OffSet = FirstStartIP + RecNo * 7
  Stream.Position = OffSet
  Buf = Stream.Read(7)
 
  EndIPOff = AscB(MidB(Buf, 5, 1)) + (AscB(MidB(Buf, 6, 1))*256) + (AscB(MidB(Buf, 7, 1))*256*256)

  StartIP  = AscB(MidB(Buf, 1, 1)) + (AscB(MidB(Buf, 2, 1))*256) + (AscB(MidB(Buf, 3, 1))*256*256) + (AscB(MidB(Buf, 4, 1))*256*256*256)
  GetStartIP = StartIP
 End Function
 ' ============================================
 ' 擷取結束IP位置
 ' ============================================
 Private Function GetEndIP()
  Stream.Position = EndIPOff
  Buf = Stream.Read(5)
  EndIP = AscB(MidB(Buf, 1, 1)) + (AscB(MidB(Buf, 2, 1))*256) + (AscB(MidB(Buf, 3, 1))*256*256) + (AscB(MidB(Buf, 4, 1))*256*256*256)

  CountryFlag = AscB(MidB(Buf, 5, 1))
  GetEndIP = EndIP
 End Function
 ' ============================================
 ' 擷取地區資訊,包含國家和和省市
 ' ============================================
 Private Sub GetCountry(IP)
  If (CountryFlag = 1 or CountryFlag = 2) Then
   Country = GetFlagStr(EndIPOff + 4)
   If CountryFlag = 1 Then
    LocalStr = GetFlagStr(Stream.Position)
    ' 以下用來擷取資料庫版本資訊
    If IP >= IPToInt("255.255.255.0") And IP <= IPToInt("255.255.255.255") Then
     LocalStr = GetFlagStr(EndIPOff + 21)
     Country = GetFlagStr(EndIPOff + 12)
    End If
   Else
    LocalStr = GetFlagStr(EndIPOff + 8)
   End If
  Else
   Country = GetFlagStr(EndIPOff + 4)
   LocalStr = GetFlagStr(Stream.Position)
  End If
  ' 過濾資料庫中的無用資訊
  Country = Trim(Country)
  LocalStr = Trim(LocalStr)
  'If InStr(Country, "CZ88.NET") Then Country = "網路保留地址。"
  'If InStr(LocalStr, "CZ88.NET") Then LocalStr = "本機內部迴路位址。"
 End Sub
 ' ============================================
 ' 擷取IP地址標識符
 ' ============================================
 Private Function GetFlagStr(OffSet)
  Dim Flag
  Flag = 0
  Do While (True)
   Stream.Position = OffSet
   Flag = AscB(Stream.Read(1))
   If(Flag = 1 or Flag = 2 ) Then
    Buf = Stream.Read(3)
    If (Flag = 2 ) Then
     CountryFlag = 2
     EndIPOff = OffSet - 4
    End If
    OffSet = AscB(MidB(Buf, 1, 1)) + (AscB(MidB(Buf, 2, 1))*256) + (AscB(MidB(Buf, 3, 1))*256*256)
   Else
    Exit Do
   End If
  Loop
 
  If (OffSet < 12 ) Then
   GetFlagStr = ""
  Else
   Stream.Position = OffSet
   GetFlagStr = GetStr()
  End If
 End Function
 ' ============================================
 ' 擷取字串資訊
 ' ============================================
 Private Function GetStr()
  Dim c
  GetStr = ""
  Do While (True)
   c = AscB(Stream.Read(1))
   If (c = 0) Then Exit Do
  
   '如果是雙位元組,就進行高位元組在結合低位元組合成一個字元
   If c > 127 Then
    If Stream.EOS Then Exit Do
    GetStr = GetStr & Chr(AscW(ChrB(AscB(Stream.Read(1))) & ChrB(C)))
   Else
    GetStr = GetStr & Chr(c)
   End If
  Loop
 End Function
 ' ============================================
 ' 核心函數,執行IP搜尋
 ' ============================================
 Public Function QQWry(DotIP)
  Dim IP, nRet
  Dim RangB, RangE, RecNo
 
  IP = IPToInt (DotIP)
 
  Set Stream = CreateObject("Adodb.Stream")
  Stream.Mode = 3
  Stream.Type = 1
  Stream.Open
  Stream.LoadFromFile QQWryFile
  Stream.Position = 0
  Buf = Stream.Read(8)
 
  FirstStartIP = AscB(MidB(Buf, 1, 1)) + (AscB(MidB(Buf, 2, 1))*256) + (AscB(MidB(Buf, 3, 1))*256*256) + (AscB(MidB(Buf, 4, 1))*256*256*256)
  LastStartIP  = AscB(MidB(Buf, 5, 1)) + (AscB(MidB(Buf, 6, 1))*256) + (AscB(MidB(Buf, 7, 1))*256*256) + (AscB(MidB(Buf, 8, 1))*256*256*256)
  RecordCount = Int((LastStartIP - FirstStartIP)/7)
  ' 在資料庫中找不到任何IP地址
  If (RecordCount <= 1) Then
   Country = "未知"
   QQWry = 2
   Exit Function
  End If
 
  RangB = 0
  RangE = RecordCount
 
  Do While (RangB < (RangE - 1))
   RecNo = Int((RangB + RangE)/2)
   Call GetStartIP (RecNo)
   If (IP = StartIP) Then
    RangB = RecNo
    Exit Do
   End If
   If (IP > StartIP) Then
    RangB = RecNo
   Else
    RangE = RecNo
   End If
  Loop
 
  Call GetStartIP(RangB)
  Call GetEndIP()

  If (StartIP <= IP) And ( EndIP >= IP) Then
   ' 沒有找到
   nRet = 0
  Else
   ' 正常
   nRet = 3
  End If
  Call GetCountry(IP)

  QQWry = nRet
 End Function
 ' ============================================
 ' 類終結
 ' ============================================
 Private Sub Class_Terminate
  On ErrOr Resume Next
  Stream.Close
  If Err Then Err.Clear
  Set Stream = Nothing
 End Sub
End Class

%>

<%
userip=request.servervariables("http_x_forwarded_for")
if request.servervariables("http_x_forwarded_for")="" then
userip=request.servervariables("remote_addr")
end if
userip = "219.146.240.252"
response.write(Look_Ip(userip))
%> 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.