ASP writes a complete IP location search class

zhaozj2021-02-17  59

Fixed the query method, the method of queries and the consistency of pursuit; just pursuing the IP automatically.

There is also a function of writing errors and has been amended;

Includes verification functions that add an IP address correctness. (Just judging from format)

<%

'Author: Xiao Han Xue (S.F.)

'QQ number: 410000

Server.scripttimeout = & HE10 '& H3C

Response.buffer = ("s.f." = "sf.")

DIM IPSEARCH

'Establish an object

Set ipsearch = new CLSIPSEARCH

'This sentence establishes the connection to the IP address library of SQL Server, you can use the default connection, but to ensure that Wry.mdb

IPsearch.connectionstring = "driver = {SQL Server}; server = hostname: uid = sa; pwd =; dataBase = IP"

'Set the IP you want to query, you can use the default value, set 127.0.0.1

IPsearch.ipaddress = & h7f & "." & "& H00 &". "& H01

If Request.QueryString ("IP") <> ""

If ipsearch.valid_ip (Request.QueryString ("IP")) THEN

IPsearch.ipaddress = trim (Request.QueryString ("IP"))

END IF

END IF

'Get the location of IP, there are three feedback values, divided by comma

'Format is: the country or region, local Internet area, user name to provide correct IP address information

Response.write ("location:" & ipsearch.getipaddrinfo () & "
")

'Remove the IP address

Response.write ("IP:" & IPSearch.ipaddress & "
")

'Convert IP addresses to values

Response.write ("IP conversion is numerical:" & ipsearch.clongip (ipsearch.ipaddress) & "
")

'Restore the IP address to the value after the IP string

Response.write ("Numeric Restore is IP:" & IPSearch.cstringIP (IPSearch.Clongip (IPSearch.ipaddress) & "
")

Response.write ("


")

'This 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 () &"
")' response.write (" IP: "& ipsearch.ipaddress &"
") 'response.write (" IP conversion is numerical: "& ipsearch.clongip (ipsearch.ipaddress) &"
")' response.write (" numerical restore Ip: "& ipsearch.cstringip (ipsearch.clongip (ipsearch.ipaddress) &"
") 'response.write ("


")' Next 'Next' Next'Next%> <% CLSIPSEARCH '######################################################## ##############> Declaration: The data adopted by this program is a well-known author "Hu Zhihong" "Mr. Feng Zhihong". '"Vault" Database Conversion Method:' Modify the Wry.dll File Suffix Name to Wry.dbf 'Method 1:' Start Access Data, Select Open Database, Select Open File Type "DBASE 5 (* .dbf)" ' Open the Wry.dbf file, select "Conversion Database" in "Database Utility" under the Tools menu to convert to Access 97 Format (Version optional) "function, save the file into the MDB format. 'Method 2:' Use the "Import and Export Data" wizard provided by SQL Server. 'Brief Description: Set the DSN to Wry.dbf in the ODBC Control Panel. 'Use the Import and Export Data Wizard, select its correct driver and the library to import. 'Or directly import the MDB file that is generated by the method. 'Method 3:' Use Access to open the wry.dbf file, will automatically use the original library data through the MDB library. '' No other database platform is installed, other methods are owed.

'########################################################################################################################################################################################################################################################################################################## ########### # i i 所 搜 类 声 声 声 数据, the default declaration of the wry.mdb '# ipaddress of the same class directory, please set to search for the IP address, default to take the current access IP '# class establishment method' # Dim objval 'declares a variable' # set objval = new clsipsearch 'Establishing class object' #RESPONSE.WRITE (OBJVAL.IPADDRESS) 'Display the current visitor ip' # ip search class method list: '# .Valid_ip' IP address correctness test '# Parameters: IP' IP value or string '# .clongip' converts IP addresses to long integer value '# parameters: asnewip' to convert IP address string ' #.Cstringip 'converts long-interalted numerical values ​​to IP' # parameters: Annewip 'To restore the value of the IP address' # .GetClientip 'take the accessient's IP' # .GetipadDrinfo 'to the IP Locate IP's IP's IP' # Property list (Auto Initialization): '# Connectionstring' ADO Access Database Connection Description '# ipaddress' To operate IP Address '# Internal Error Process:' #, do not do, please add it. '######################################################## #################

Public ConnectionstringPublic ipaddressPrivate Dbconn 'connection objects, modular grade declarations - - ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────> () 'The MDB library file created here is converted by "Data Transition - Method 1" Connectionstring = "Driver = {Microsoft Access Driver (* .mdb)}; DBQ =" & Server.MAppath ("Wry.mdb" Ipaddress = getClientip () set dbconn = OpenConnection () end sub' ─────────────────────────────────── ─ ─ ─ ─ ─ ─ Private sub coplass_terminate () ConnectionsTRING = nullipaddress = nulldbconn.closeset dbconn = Nothingend Sub' - ─────────────────────────────────────── establishing a connection Private Function OpenConnection () Dim tmpConn.Open ConnectionStringSet OpenConnection tmpConnSet tmpConn = Server.CreateObject ( "ADODB.Connection") = tmpConnSet tmpConn = nothingEnd Function'──────────────── ──────── ── 'Execute a SQL command, and return a dataset object private function sqlexecute (strsql) DIM RSSET RS = dbconn.execute (strsql) set sqlexecute = rsset = Nothingend Function' --──────────────────────────'ip Test PUBLIC FUNCTION VALID_IP (BYVAL IP) DIM I DIM DOT_COUNT DIM Test_OCTET DIM BYTE_CHECK IP = TRIM (IP) 'Confirm the IP length IF LEN (IP) <& H08 Then Valid_ip = false 'display error prompt EXIT FUNCTION END IF

I = & h01 dot_count = & h00 for i = 1 to LEN (IP) IF MID (IP, I, & H01) = "." THEN 'increasing point of the count value "and sets the text_octet value empty dot_count = dot_count & h01 test_octet =" "If i = len (ip) Then 'If the point is at the end, the IP papers failed valid_ip = false' display error prompt EXIT FUNCTION END IF ELSE TEST_OCTET = TEST_OCTET & MID (IP, I, & H01) 'Using Error Shield to check data segment The correctness of the value on ERROR RESUME NEXT 'Make Force Type Conversion' If the conversion fails, you can confirm whether the ERR is true to confirm whether Byte_Check = Cbyte (Test_OCTET) IF (ERR) Ten 'forced Type Conversion Generate Errors' Betting Value The data length of data is not values' or the value of the value is greater than the value of the value of the value is the correctness of the BYTE type 'IP address is the false valid_ip = false exit function end if end if next' pass the previous verification, it should now Check if there is 3 if dot_count <> & h03 kilid_ip = false exit function end if 'everything is normal, then the IP is the correct IP address valid_ip = true end function' - --───────────── ───────────── - 'Convert a value for ippublic function cstringip (Byval Annewip) DIM LSRESULTSDIM LNTEMPDIM LNINDEXFOR LNINDEX = & H03 TO & H00 Step - & H01LnTemp = Int (Annewip / & H100 ^ lnindex) LsResults = lsresults & lntemp & "Annewip = AnnewIP - (Lntemp * (& H100 ^ lnIndex) NextLsResults = Left (LsResults, Len (LsResults) - & H01) CStringIP = lsresultsend function' --─────────────────────── ─ ─ ─ 'Convert an IP to Numerical PUBLIC FUNCTION Clongip (ByVal asNewIP) Dim lnResultsDim lnIndexDim lnIpArylnIpAry = Split ( "." asNewIP,, & H04) For lnIndex = & H00 To & H03if Not lnIndex = & H03 ThenlnIpAry (lnIndex) = lnIpAry (lnIndex) * (& H100 ^ (& H03 - lnIndex)) End iflnResults = lnResults lnipary (lnIndex) nextclongip = lnResultsend Function' - ────────────────────────────

Take Client IPPublic Function GetClientIP () dim uIpAddr 'webcn.Net/AspHouse present Function Reference Document uIpAddr = Request.ServerVariables ( "HTTP_X_FORWARDED_FOR") If uIpAddr = "" Then uIpAddr = Request.ServerVariables ( "REMOTE_ADDR ") GetClientip = uipaddruipaddr =" "end function' - ─────────────────── ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ public function GetIpAddrInfo () Dim tmpIpAddrDim IpAddrValDim ic, charSpaceDim tmpSQLcharSpace = "" IpAddrVal = IpAddressIf Not Valid_IP (IpAddrVal) ThenGetIpAddrInfo = NULLExit FunctionEnd If 'will be split into an array of strings IP good processing tmpIpAddr = Split (IpAddrVal, ".", -1,1) for IC = & h00 to ubound (tmpiPaddr) 'Adjustment operation, guarantees 3 characters to SELECT CASE LEN (TmpipAddr (IC)) Case & H01: Charspace = "00" 0 "Case & H02: Charspace =" 0 "Case Else: charSpace =" "." "." "." "End SelecttmpIpAddr (ic) = charSpace & tmpIpAddr (ic) NextIpAddrVal = tmpIpAddr (& H00) & & tmpIpAddr (& H01) & & tmpIpAddr (& H02) & & tmpIpAddr (& H03)

'The following is a query, IP address library based on "pursue" IP database, thanks to Mr. Feng Zhihong,' The library 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, - Termination IP Segment' [Country] [NVARCHAR] [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 to the IP address User Name ') ON [primary]' Data storage structure of the analysis library Summarize the accurate query method, specifically look at the Query process of the following query 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 ThenGetIpAddrInfo = NULLElseGetIpAddrInfo = charSpaceEnd IfcharSpace = NulltmpSQL = Nullend function'───────────── ──────────── ─ 'Return Data Inquiry String Private Function GetDBipInfo (Byval SQL) DIM OPENIPSEARCHRSDIM RESULTSET OPENIPSEARCHR s = SQLExeCute (sql) If Not OpenIpSearchRs.Eof Thenresult = NullToSpace (OpenIpSearchRs ( "COUNTRY")) & "," & NullToSpace (OpenIpSearchRs ( "LOCAL")) & "," & NullToSpace (OpenIpSearchRs ( "THANK")) Elseresult = Nullend IfOpenIPSearchrs.closset OpenIPSearchrs = NothingGetDBipinfo = Resultens Function' - ──────────────────────────────────────────

转载请注明原文地址:https://www.9cbs.com/read-29936.html

New Post(0)