Compile a complete IP address location search class in ASP

Source: Internet
Author: User

 

<%
Server. ScriptTimeout = & he10' & H3C
Response. Buffer = ("S. F." = "S. F .")
Dim IpSearch
'Create an object
Set IpSearch = New clsIpSearch
'Use the default connection to establish a connection to the SQL Server IP address library, but ensure that wry. mdb exists.
IpSearch. ConnectionString = "DRIVER = {SQL Server}; SERVER = hostname: UID = sa; PWD =; DATABASE = Ip"
'Set the IP address to be queried. The default value is available. 127.0.0.1 is set here.
IpSearch. IpAddress = & H7F & "." & H00 & "." & H00 & "." & H01
If Request. QueryString ("IP") <> "Then
If IpSearch. Valid_IP (Request. QueryString ("IP") Then
IpSearch. IpAddress = Trim (Request. QueryString ("IP "))
End If
End If
'Obtain the IP address location. There are three feedback values separated by commas (,).
The format is: the user name that provides the correct IP address information in the country or region where the local network is located.
Response. Write ("Location:" & IpSearch. GetIpAddrInfo () & "<br/> ")
'Retrieve the IP address
Response. Write ("IP:" & IpSearch. IpAddress & "<br/> ")
'Convert the IP address to a numeric value
Response. Write ("Convert IP to value:" & IpSearch. CLongIP (IpSearch. IpAddress) & "<br/> ")
'Convert the IP address to a numeric value and then restore it to an IP string.
Response. Write ("restoring the value to IP:" & IpSearch. CStringIP (IpSearch. CLongIP (IpSearch. IpAddress) & "<br/> ")
Response. Write ("

'Here is the test code.
'Dim a, B, c, d
'For a = 0 to 255
'For B = 0 to 255 step 20
'For c = 0 to 255 step 20
'For d = 0 to 255 step 20
'Ipsearch. IpAddress = a & "." & B & "." & c & "." & d
'Response. Write ("Location:" & IpSearch. GetIpAddrInfo () & "<br/> ")
'Response. Write ("IP:" & IpSearch. IpAddress & "<br/> ")
'Response. Write ("Convert IP to value:" & IpSearch. CLongIP (IpSearch. IpAddress) & "<br/> ")
'Response. Write ("restoring the value to IP:" & IpSearch. CStringIP (IpSearch. CLongIP (IpSearch. IpAddress) & "<br/> ")
'Response. Write ("'Next
'Next
'Next
'Next
%>
<%
Class clsIpSearch
'####################################### ###########################
'Statement: The data used in this program is the well-known IP tool software on the network, "hunt down" by "Feng Zhihong"
'Mr. collected carefully.
'The method for converting the hunting database:
'Modify the suffix of wry. dll file to wry. dbf.
'Method 1:
'Start Access data, select Open Database, and select the open file type as "dBASE 5 (*. dbf )"
'Open the wry. dbf file and select "Convert Database" in "database utility" under "Tools".
'Select the convert to Access 97 format (available in version) function and save the file to become the MDB format.
'Method 2:
'Use the import and export data Wizard provided by SQL Server.
'Method Brief description: In the ODBC control panel, set the DSN pointing to wry. dbf.
'Use the import and export data wizard and select the correct driver and the library to import.
'Or directly import the MDB file generated in the lifetime of the method into the database.
'Method 3:
'After you use Access to open the wry. dbf file, the source database data is automatically referenced through the MDB library.
'
'The other database platforms are not installed. Other methods are not considered.
############### #####################
'# IP address location search
'# ConnectionString indicates the database connection statement. wry. mdb of the same level directory is declared by default.
'# Set IpAddress to the IP address for search. The current visitor's IP address is used by default.
'# Class Creation Method
'# Dim objVal' declares a variable
'# Set objVal = New clsipsearch' to create a Class Object
'# Response. Write (objVal. IpAddress)' displays the IP address of the Current Visitor.
'# List of IP search methods:
'#. Valid_IP' IP address correctness Verification
'# Parameter: IP' IP value or string
'#. Clongip' converts an IP address to a long integer.
'# Parameter: asnewip' the IP address string to be converted
'#. Cstringip' converts a long integer value to an IP address.
'# Parameter: annewip' the value to be restored to the IP address
'#. Getclientip' gets the visitor's IP address
'#. GetIpAddrInfo' to obtain the IP address location with the IpAddRess attribute set.
'# Attribute list (automatic initialization ):
'# ConnEctionString' ADo database access connection description
'# Ipaddress' IP address to be operated
'# Internal error handling:
'# Missing, not done. Please add it by yourself.
'####################################### ###########################

Public ConnectionString
Public IpAddress
Private dbconn' connection object, module-Level Declaration
'── ─
'Class initialization
Private Sub Class_initialize ()
'The created here is the mdb library file generated through "data conversion-method 1"
ConnectionString = "DRIVER = {Microsoft Access Driver (*. mdb)}; DBQ =" & Server. MapPath ("wry. mdb ")
IpAddress = GetClientIP ()
Set DBConn = OpenConnection ()
End Sub
'── ─
'Class logout
Private Sub Class_Terminate ()
ConnectionString = Null
IpAddress = Null
DBConn. Close
Set DBConn = Nothing
End Sub
'── ─
'Create a connection
Private Function OpenConnection ()
Dim tmpConn
Set tmpConn = Server. CreateObject ("ADODB. Connection ")
TmpConn. Open ConnectionString
Set OpenConnection = tmpConn
Set tmpConn = nothing
End Function
'── ─
'Execute an SQL command and return a DataSet object
Private Function SQLExeCute (strSql)
Dim Rs
Set Rs = DBConn. ExeCute (strSQL)
Set SQLExeCute = Rs
Set Rs = nothing
End Function

 

 

Compile a complete IP address location search class in ASP (2)
------------------------------------
'── ─
'IP Verification
Public Function Valid_IP (ByVal IP)
Dim I
Dim dot_count
Dim test_octet
Dim byte_check
IP = Trim (IP)
'Confirm the IP address Length
If Len (IP) <& H08 Then
Valid_IP = False
'Display error message
Exit Function
End If

I = & H01
Dot_count = & H00
For I = 1 To Len (IP)
If Mid (IP, I, & H01) = "." Then
'Add the vertex value.
'And the value of text_octet is set to null.
Dot_count = dot_count + & H01
Test_octet = ""
If I = Len (IP) Then
'IP verification fails if the point is at the end.
Valid_IP = False
'Display error message
Exit Function
End If
Else
Test_octet = test_octet & Mid (IP, I, & H01)
'Use Error Blocking to check the correctness of the Data Segment Value
On Error Resume Next
'Forced type conversion
'If the conversion fails, check whether Err is true.
Byte_check = CByte (test_octet)
If (Err) Then
'Forced type conversion produces an error
'The data of the retrieved segment value is not a numerical value
'Or the Data Length of the obtained segment value is greater than & HFF
'The type is not byte
'IP address correctness is false
Valid_IP = False
Exit Function
End If
End If
Next

'After the verification in the previous step, you should check whether there are 3 small dots
If dot_count <> & H03 Then
Valid_IP = False
Exit Function
End If
'If everything is normal, the IP address is the correct IP address.
Valid_IP = True
End Function
'── ─
'Convert a value to IP
Public Function CStringIP (ByVal anNewIP)
Dim lsResults
Dim lnTemp
Dim lnIndex
For lnIndex = & H03 To & H00 Step-& H01
LnTemp = Int (anNewIP/(& H100 ^ lnIndex ))
LsResults = lsResults & lnTemp &"."
AnNewIP = anNewIP-(lnTemp * (& H100 ^ lnIndex ))
Next
LsResults = Left (lsResults, Len (lsResults)-& H01)
CStringIP = lsResults
End function
'── ─
'Convert an IP address to a numeric value
Public Function CLongIP (ByVal asNewIP)
Dim lnResults
Dim lnIndex
Dim lnIpAry
LnIpAry = Split (asNewIP, ".", & H04)
For lnIndex = & H00 To & H03
If Not lnIndex = & H03 Then
LnIpAry (lnIndex) = lnIpAry (lnIndex) * (& H100 ^ (& H03-lnIndex ))
End if
LnResults = lnResults + lnIpAry (lnIndex)
Next
CLongIP = lnResults
End function
'── ─
'Obtain the Client IP Address
Public Function GetClientIP ()
Dim uIpAddr
'For this function, see the webcn. Net/AspHouse document <obtain the real customer IP address>
UIpAddr = Request. ServerVariables ("HTTP_X_FORWARDED_FOR ")
If uIpAddr = "" Then uIpAddr = Request. ServerVariables ("REMOTE_ADDR ")
GetClientIP = uIpAddr
UIpAddr = ""
End function
'── ─
'Read the IP address location information
Public function GetIpAddrInfo ()
Dim tmpIpAddr
Dim IpAddrVal
Dim ic, charSpace
Dim tmpSQL
CharSpace = ""
IpAddrVal = IpAddress
If Not Valid_IP (IpAddrVal) Then
GetIpAddrInfo = NULL
Exit Function
End If
'Split the IP string into an array for processing.
TmpIpAddr = Split (IpAddrVal, ".",-1, 1)
For ic = & H00 To Ubound (tmpIpAddr)
'Fill operation to ensure that each interval contains 3 Characters
Select Case Len (tmpIpAddr (ic ))
Case & H01: charSpace = "00"
Case & H02: charSpace = "0"
Case Else: charSpace = ""
End Select
TmpIpAddr (ic) = charSpace & tmpIpAddr (ic)
Next
IpAddrVal = tmpIpAddr (& H00) & "." & tmpIpAddr (& H01) & "." & tmpIpAddr (& H02) & "." & tmpIpAddr (& H03)

'The following is a query. The IP address library is based on the IP address database of hunt. Thanks to Mr. Feng Zhihong for his contribution.
'The database structure is as follows:
'Create TABLE [dbo]. [wry] (
'[STARTIP] [nvarchar] (17) COLLATE Chinese_PRC_CI_AS NULL, -- start IP segment
'[ENDIP] [nvarchar] (17) COLLATE Chinese_PRC_CI_AS NULL, -- terminate the IP segment
'[COUNTRY] [nvarchar] (16) COLLATE Chinese_PRC_CI_AS NULL, -- COUNTRY or region
'[LOCAL] [nvarchar] (54) COLLATE Chinese_PRC_CI_AS NULL, -- LOCAL address
'[THANK] [nvarchar] (23) COLLATE Chinese_PRC_CI_AS NULL -- thanks for modifying the IP address user name
') ON [PRIMARY]
'The data storage structure of the analysis database summarizes the accurate query methods. For details, refer to the following query process.
TmpSQL = "select * from wry where (startIP <= '" & IpAddrVal & "') and (ENDIP> = '" & IpAddrVal &"')"&_
"And left (startIP," & Len (tmpIpAddr (& H00) & ") = '" & tmpIpAddr (& H00 )&"'"&_
"And left (endip," & Len (tmpIpAddr (& H00) & ") = '" & tmpIpAddr (& H00 )&"'"
CharSpace = GetDbIpInfo (tmpSQL)
If Len (charSpace) = & H00 Then
GetIpAddrInfo = NULL
Else
GetIpAddrInfo = charSpace
End If
CharSpace = Null
TmpSQL = Null
End function
'── ─
'Return the data query string
Private function GetDbIpInfo (byVal SQL)
Dim OpenIpSearchRs
Dim result
Set OpenIpSearchRs = SQLExeCute (SQL)
If Not OpenIpSearchRs. Eof Then
Result = NullToSpace (OpenIpSearchRs ("COUNTRY") & "," & NullToSpace (OpenIpSearchRs ("LOCAL "))&","&
NullToSpace (OpenIpSearchRs ("THANK "))
Else
Result = NULL
End If
OpenIpSearchRs. Close
Set OpenIpSearchRs = Nothing
GetDbIpInfo = result
End function
'── ─
'Convert a null record in the database to a null character
Private function NullToSpace (byVal rsStr)
If isNull (rsStr) Then
NullToSpace = ""
Else
NullToSpace = Trim (rsStr)
End If
End Function
End Class
%>

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.