Forum Moderators: open

Message Too Old, No Replies

vbScript for IP address ranges

Has anyone seen this?

         

dataguy

7:09 pm on Apr 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would like to filter certain IP address ranges from viewing certain content, but I have wasted a lot of time trying to make a function that checks if an IP address is within a certain range.

Is there a better way of doing this? I would like to be able to specific the upper and lower limites, and possibly wildcard characters.

Thanks...

emsaw

8:01 pm on Apr 8, 2005 (gmt 0)

10+ Year Member



Dataguy,

this will get you most of the way toward what you are looking for(i think):


'
' Had to put comment marks on the empty lines in order for the code show correctly here in the forums
'
<%@ Language="VBScript" %>
<% OPTION EXPLICIT %>
<%
Dim userIP, ipRange, ip
Dim lowerBound, upperBound
Dim block
block = false
userIP = Request.ServerVariables("REMOTE_ADDR")
'userIP = "127.0.2.3" 'for your testing
'
Set ip = Server.CreateObject("Scripting.Dictionary")
Set ipRange = Server.CreateObject("Scripting.Dictionary")
'
'add individual IP addresses here to block
ip.Add "", "127.0.0.1"
'
'add a partial IP address here to block .. just does simple 'Left' matching
ipRange.Add "", "127.0.0."
'
Dim key
'
For each key in ip.Keys
If( ip.Item(key) = userIP ) Then
Response.Write("blocked specific IP")
block = true
End If
Next
'
For each key in ipRange.Keys
If( ipRange.Item(key) = Left(userIP, Len(ipRange.Item(key))) ) Then
Response.Write("blocked IP range")
block = true
End If
Next
'
If block Then
Response.Write "<br>block and send to alternate url or send alternate header information"
'Response.Redirect "blockedUserPage.asp"
Else
'Do nothing here
'Response.Write "<br>no block"
End If
%>

HTH,

Mark

dataguy

8:34 pm on Apr 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks... This gives me a good start.

mrMister

6:22 am on Apr 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Regular expressions are good for this kind of job.