ASP reads IP library (DAT file)

Source: Internet
Author: User
Tags servervariables

<%
'================================================ =====
'Return IP information
'================================================ =====
Function look_ip (IP)
Dim wry, iptype, qqwryversion, ipcounter
'Set the Class Object
Set wry = new tqqwry
'Start searching and return the search result
'You can determine whether the IP address exists in the Database Based on the return value of qqwry (IP). If it does not exist, you can perform other operations.
'For example, if you have a self-built database for tracking, I will not detail it here.
Iptype = wry. qqwry (IP)
'Country: country/region Field
'Localstr: province, city, and other information fields
Look_ip = wry. Country & "& wry. localstr
End Function
'================================================ =====
'Return IP information JS call
'================================================ =====
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 ("" You are from: "& IP &" region: "& wry. country & "& wry. localstr &""");"
End select
End Function
'================================================ =====
'Return qqwry information (qqwry. DAT version and number of records)
'================================================ =====
Function wryinfo ()
Dim wry, iptype, qqwry (1)
'Set the Class Object
Set wry = new tqqwry
Iptype = wry. qqwry ("255.255.255.255 ")
'Read database version information
Qqwry (0) = wry. Country & "& wry. localstr
'Number of read database IP addresses
Qqwry (1) = wry. recordcount + 1
Wryinfo = qqwry
End Function
'================================================ =====
'IP physical location search
'================================================ =====
Class tqqwry
'================================================ =====
'Variable name
'================================================ =====
Dim country, localstr, Buf, offset
Private startip, endip, countryflag
Public qqwryfile
Public firststartip, laststartip, recordcount
Private stream, endipoff
'================================================ =====
'Class module initialization
'================================================ =====
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 address library path, to be converted to physical path
End sub
'================================================ =====
'IP address to integer
'================================================ =====
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
'================================================ =====
'Integer reverse IP Address
'================================================ =====
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
'================================================ =====
'Obtain the starting IP address location
'================================================ =====
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
'================================================ =====
'Get the end IP address location
'================================================ =====
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
'================================================ =====
'Obtain region information, including country and province/City
'================================================ =====
Private sub getcountry (IP)
If (countryflag = 1 or countryflag = 2) then
Country = getflagstr (endipoff + 4)
If countryflag = 1 then
Localstr = getflagstr (stream. position)
'The following is used to obtain the database version information.
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
'Filter useless information in the database
Country = trim (country)
Localstr = trim (localstr)
'If instr (country, "cz88.net") Then Country = "network reserved address. "
'If instr (localstr, "cz88.net") Then localstr = "local loopback address. "
End sub
'================================================ =====
'Get the IP address identifier
'================================================ =====
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
'================================================ =====
'Get string Information
'================================================ =====
Private function getstr ()
Dim C
Getstr = ""
Do While (true)
C = ASCB (stream. Read (1 ))
If (C = 0) Then exit do

'If it is a double byte, the high byte is combined with the low byte to synthesize a character
If C & gt; 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
'================================================ =====
'Core function, execute IP search
'================================================ =====
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)
'No IP address is found in the database
If (recordcount <= 1) then
Country = "unknown"
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
'Not Found
Nret = 0
Else
'Normal
Nret = 3
End if
Call getcountry (IP)

Qqwry = nret
End Function
'================================================ =====
'Class termination
'================================================ =====
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 ))
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.