Code used by Asp to restrict IP Access to prevent a certain IP segment from accessing this site

Source: Internet
Author: User
Tags servervariables

Copy codeThe Code is as follows: <%
''Get the visitor's address
Ip = Request. ServerVariables ("REMOTE_ADDR ")
''The allowed IP address range is 10.0.0.0 ~ 10.68.63.255
Allowip1 = "10.0.0.0"
Allowip2 = "10.68.10.71"
Response. writecheckip (ip, allowip1, allowip2)
Functioncheckip (ip, allowip1, allowip2)
Dimcheck (4)
Checkip = false
Ipstr = split (ip ,".")
Allow1 = split (allowip1 ,".")
Allow2 = split (allowip2 ,".")
Ifcint (allow1 (0)> cint (allow2 (0) then''
Response. write "Access prohibited"
Exitfunction
Endif
Fori = 0 toubound (ipstr)
Ifcint (allow1 (I) <cint (allow2 (I) then
Ifcint (allow1 (I) = cint (ipstr (I) then
Check (I) = true
Checkip = true
Exitfor
Else
Ifcint (ipstr (I) <cint (allow2 (I) then
Check (I) = true
Checkip = true
Exitfor
Else
Ifcint (ipstr (I)> cint (allow2 (I) then
Check (I) = false
Checkip = false
Exitfor
Else
Check (I) = true
Checkip = true
Endif
Endif
Endif
Else
Ifcint (allow1 (I)> cint (ipstr (I) orcint (allow1 (I) <cint (ipstr (I) then
Check (I) = false
Checkip = false
Ifi <> ubound (ipstr) then
Exitfor
Endif
Else
Check (I) = true
Endif
Endif
Next
If (check (0) = trueandcheck (1) = trueandcheck (2) = trueandcheck (3) = false) and (cint (allow2 (2)> cint (ipstr (2) then
Checkip = true
Endif
Endfunction
%>

Add the following code to your ASP page to test the effect:Copy codeThe Code is as follows: <%
'The set of blocked IP addresses (segments). The asterisk is a wildcard and is usually stored in the configuration file.
Const BadIPGroup = "192.168.1. * | 202. 68. *. * | *. 12.55.34 | 185. *. 96.24 | 127. *. 0.1 | 192.168.0.1"
If IsForbidIP (BadIPGroup) = True Then
Response. Write (GetIP & "Access prohibited from IP addresses ")
Response. End ()
End If
'Parameter vBadIP: IP segment to be blocked, IP address set. Use the | symbol to separate multiple IP addresses (segments)
'Return Bool: True: the user IP address is in the blocked range; False: otherwise
Function IsForbidIP (vBadIP)
Dim counter, arrIPPart, arrBadIP, arrBadIPPart, I, j
ArrBadIP = Split (vBadIP, "| ")
ArrIPPart = Split (GetIP (),".")
For I = 0 To UBound (arrBadIP)
Counter = 0
ArrBadIPPart = Split (arrBadIP (I ),".")
For j = 0 To UBound (arrIPPart)
If (arrBadIPPart (j) = "*" or Cstr (arrIPPart (j) = Cstr (arrBadIPPart (j) Then
Counter = counter + 1
End If
Next
If counter = 4 Then
IsForbidIP = True
Exit Function
End If
Next
IsForbidIP = False
End Function
''Return the customer IP Address
Function GetIP ()
Dim IP
IP = Request. ServerVariables ("HTTP_X_FORWARDED_FOR ")
If IP = "" Then IP = Request. ServerVariables ("REMOTE_ADDR ")
GetIP = IP
End Function
%>

In this way, you can restrict the IP address segments for Website access. You can set the IP address segments based on the region.
I used this method to prevent users from using rogue software to maliciously publish information to my website!

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.