A global IP address search program written in pure ASP (VBscript)

Source: Internet
Author: User
Tags get ip servervariables split trim client
IP Address |vbscript| Program < "codepage=" 936 "%>"
<title>untitled document</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body>
<%
Dim finishgetip,showip,allip
''////////////////////////////////////////////////////////////////////////////////////////
"The procedure is not very concise, and later modified
' The database used for this program is--"Feng Zhihong"--"hunt"--the IP database in the software and
The author of "Feng"-"The Global IP Address assignment table. CHM"-Written by "Soft", "Guohua Software"
"Thanks to the data provided by" Feng Zhihong "and" Feng "
"There are a lot of duplicate IP address in the database, I hope to be able to remove it, reduce the database
"My program is very clumsy to write, I hope we can make more comments, a lot of exchanges, thank you!"
''////////////////////////////////////////////////////////////////////////////////////////
"The solution to the idea:
"The client IP obtained is typically 202.11.25.1 this, and the IP format in the database is 202.011.025.001, which requires the resulting
' client IP is converted to the same format as the IP in the database
"Because at the moment we are using the IP is divided into 4 segments, 3 bits per paragraph, the middle of the". " Separated
"So my idea is to have the client IP". " The symbol is divided into 4 paragraphs, namely 202/11/25/1
"And then check each paragraph separately, if it is 3 digits, the same, if less than 3 digits, 2 bits, 1 0, 1 for the same paragraph, then 2 0
"After the formatted IP, remove the last paragraph of IP, which includes". " The top 11 digits of the StartIP field in the database, and look for the same value compared to the first 11 digits of the
Because you can see from the database that the first three paragraphs of StartIP and EndIP are the same, and the last paragraph is just an internal subnet address, you can remove
"So you can find the correct location by comparing the first 11 bits of any field in StartIP or EndIP with the top 11 of the client IP
''/////////////////////////////////////////////////////////////////////////////////////////
function Checkip_trueip ()
"Take client real IP
Getclientip = Request.ServerVariables ("http_x_forwarded_for") "If the client is using a proxy server, use Request.ServerVariables (" REMOTE_ ADDR ") method can only get null values, you should use the ServerVariables (" Http_x_forwarded_for ") method
If Getclientip = "" Then
Getclientip = Request.ServerVariables ("remote_addr") "" Request.ServerVariables ("http_x_forwarded_for") if the client does not use an agent Get null value, you should use Request.ServerVariables ("REMOTE_ADDR") method
End If
CHECKIP_TRUEIP = Getclientip
End Function
''/////////////////////////////////////////////////////////////////////////////
function Getaccessrecordset (db,sql,mark,read) ' Gets the Recordset object
Set Conn=getaccessconn (db) ' input parameter is the relative path of db-database, Sql-sql statement, Mark,read for database read mode, 1,1 read-only, 1,3 for read-write
' Constr= ' provider=microsoft.jet.oledb.4.0; & "Data source=" &server.mappath (db)
"' Conn.Open Constr
Set Getaccessrecordset=server. CreateObject ("ADODB.") Recordset ")
Getaccessrecordset.open Sql,conn,mark,read
End Function
''//////////////////////////////////////////////////////////////////////////
function getaccessconn (db) ' Gets connection object
Set Getaccessconn=server. CreateObject ("ADODB.") Connection ")
' constr= ' Driver={microsoft ACCESS DRIVER (*. MDB)};D bq= "&server. MAPPATH ("allcon/#bbsall. mdb")
Constr= "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data source=" &server.mappath (db)
Getaccessconn.open Constr
End Function
''/////////////////////////////////////////////////////////////////////////
Dim getip
' Getip= (the trim (request). ServerVariables ("REMOTE_ADDR")) "Get IP from client
' Getip= (the trim (request). QueryString ("Comes")) "Enter the IP Test yourself
"Response." Write (getip& "<br>")
''////////////////////////////////////////////////////////////////////////
function checkip_locations (checkstring) ' Returns the positional functions of delimited characters in IP
Checkip_locations=instr (Checkstring, ".") " Assigning the value of a position to a function
End Function
''///////////////////////////////////////////////////////////////////////
The following function is to split the IP and get each partition "." The IP remaining string to the right of the symbol
function Checkip_left (checkstring)
Locations_left=checkip_locations (checkstring) ' Gets the "." In the IP-remaining string. The first occurrence of the position
Iplength_left=len (checkstring) ' Gets the length of the string remaining in the IP
Divide_locations_left=iplength_left-locations_left ' Gets the "." In the IP-remaining string. The first occurrence of the position, from the right to the left number of bits
Ipstr_left=right (checkstring,divide_locations_left) ' Get this split, "." The IP remaining string to the right of the symbol
Checkip_left=ipstr_left ' "assigns the resulting string to the function
End Function
''///////////////////////////////////////////////////////////////////////
The following function is to split the IP and get each partition "." The IP string to the left of the symbol, which divides the IP into four segments and the string of each segment

function Checkip_right (checkstring)
Locations_right=checkip_locations (checkstring) ' Get in IP '. The first occurrence of the position
Iplength_right=len (checkstring) ' Gets the length of the IP string
Divide_locations_right=iplength_right-locations_right ' Gets the "." In the IP-remaining string. The first occurrence of the position, from the right to the left number of bits
Ipstr11=trim (Replace (left (checkstring,locations_right), ".", "")) ' will get. ' The string on the left removes the "." Symbol
"If the IP is divided into 4 paragraphs after each section of less than 3 digits to fill 0
If Len (IPSTR11) = "2" then ipstr11= "0" &ipstr11
If Len (IPSTR11) = "3" then Ipstr11=ipstr11
If Len (IPSTR11) = "1" then ipstr11= "&ipstr11"
Checkip_right=ipstr11 ' get '. " The string before the symbol, that is, the IP points obtained after this partition



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.