Asp restricts IP access and prevents a certain IP segment from accessing the code of this site.
Often some local B2B or B2C websites have a lot of spam posted in batches everywhere. I would like to share my solution with everyone.
The 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'' determines whether the IP address segment is legal
response.writeForbidden access
exit function
endif
fori=0toubound(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 the code code as follows:
<%
'A collection of blocked IP addresses (segments), asterisk is a wildcard character, usually saved 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 &IP address is prohibited from access)
Response.End()
End If
'Parameter vBadIP: IP segment to be blocked, IP address set, use | symbol to separate multiple IP addresses (segments)
'Return Bool: True the user IP 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 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 range for website access. You can set it according to the IP range of the region.
I use this method to prevent foreign users from using rogue software to maliciously post information on my website!