We help IT Professionals succeed at work.
Get Started

Redirect users based on ip address ranges help in sql database

edcerv
edcerv asked
on
455 Views
Last Modified: 2012-05-10
Hi, I run a website written in classic asp. Certain members would like to be able to access the members area of the site by using ip address ranges, which I'd like to store in a table in a SQL db using the following or similar format to explain the allowed ranges:

129.130.*.*
193.60.128-143.*

I found some code which almost does what I want, except that it doesn't explain how to deal with values if in a db table. I also don't understand how to call the function. Here's the code I found:

<%
'**************************************
'(c) 2000-2002 by Richard Burwood
'Contact: development@rburwood.com
'**************************************
Function allowAccess(testIPstr)
'
'**************************************
'YOU SHOULD ONLY NEED TO EDIT THE VALUE
'OF THE VARIABLES / CONSTANTS IN THIS
'AREA TO GET THIS FUNCTION TO WORK!!!!
'**************************************
'Adjust the number of items in the
'array to reflect the number of rules
'you are testing
Dim IPrules(2)
'The ip rule can either be a single ip
'address or it can contain the *
'character used as a wildcard. You can
'specify a range of IP addresses by
'using the following syntax,
'"192.168.0.5-10" See examples below...
'Example Rules:
'    "127.0.0.*"
'    "192.168.0.1-40"
'    "192.168.0.*"
'    "192.168.*.*"
'    "192.168.0-10.*"
'    "127.0.0.1"
'
'These rule definitions could very
'easily be in a database or txt file
IPrules(0)= "127.0.0.*"
IPrules(1)= "192.168.0.1-50"
'If the function can't obtain the IP
'address should the function return
'allow access?
'Possible Values = "Allow" or "Refuse"
IPErr = "Refuse"
'Does the function return True or False
'if the IP address being tested is
'within the rule?
'Possible Values = "True" or "False"
RuleType = "True"
'************************************
'There are two server variables that
'we use to obtain the users IP address:
'REMOTE_ADDR and HTTP_X_FORWARDED_FOR.
'Most web users access the internet
'via a third party (their ISP),
'REMOTE_ADDR does not always contain
'their IP address. It can contain their
'ISP's IP address. If it does, most
'browsers will then store the users IP
'address in the HTTP_X_FORWARDED_FOR
'variable. So we first check the
'HTTP_X_FORWARDED_FOR, and then if
'that is empty, we will try to use
'REMOTE_ADDR.
    If testIPstr = "" Then
        testIPstr = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
        If testIPstr ="" Then
            testIPstr = Request.ServerVariables("REMOTE_ADDR")
        End If
    End If
    If testIPstr ="" Then
        ' Can't Obtain user IP address
        If IPErr = "Allow" Then
            allowAccess = "True"
        Else
            allowAccess = "False"
        End If
    Else
        testIP = Split(testIPstr,".")
        'testIP(0) = 1st octet of test ip address
        'testIP(1) = 2nd octet of test ip address
        'testIP(2) = 3rd octet of test ip address
        'testIP(3) = 4th octet of test ip address
       
        'Declare the result array
        Dim IPresult(4)
        'Loop through the array of rules and test the IP address against them
        ctr1 = 0
        Do While ctr1 < UBound(IPRules)
            accessIP = Split(IPRules(ctr1),".")
           
            'accessIP(0) = 1st octet of the ip rule
            'accessIP(1) = 2nd octet of the ip rule
            'accessIP(2) = 3rd octet of the ip rule
            'accessIP(3) = 4th octet of the ip rule
                       
            'We will now Loop through the ip octets and compare them.
            'If the ip rule octet is equal an asterix Then it is an immediate
            'match. if the access octet contains the minus character Then the
            'octet is a range of addresses. All of the octets must return a
            'match For the function To be certain that the IP address being
            'tested is a match.
           
            ctr2 = 0
            Do While ctr2 < 4
                If accessIP(ctr2) <> "*" Then
                    If InStr(accessIP(ctr2), "-") > 0 Then
                        accessLR = Left(accessIP(ctr2) , InStr(accessIP(ctr2) , "-") - 1)
                        accessUR = Right(accessIP(ctr2) , Len(accessIP(ctr2) ) - InStr(accessIP(ctr2), "-"))
                        If testIP(ctr2) => accessLR And testIP(ctr2) <= accessUR Then
                            IPresult(ctr2) = "Match"
                        Else
                            IPresult(ctr2) = "NoMatch"
                        End If
                    Else
                        If testIP(ctr2) = accessIP(ctr2) Then
                            IPresult(ctr2) = "Match"
                        Else
                            IPresult(ctr2) = "NoMatch"
                        End If
                    End If
                Else
                    IPresult(ctr2) = "Match"
                End If
                ctr2 = ctr2 + 1
            Loop
           
            'This function can be used either To allow access or restrict access to the
            'specified accessIP address / range. If the function is True Then there is no
            'need To check further rules so Exit loop.
           
            If IPresult(0) = "Match" And IPresult(1) = "Match" And IPresult(2) = "Match" And IPresult(3) = "Match" Then
                If RuleType = "True" Then
                    allowAccess = "True"
                    Exit Do
                Else
                    allowAccess = "False"
                End If          
            Else
                If RuleType = "True" Then
                    allowAccess = "False"
                Else
                    allowAccess = "True"
                    Exit Do
                End If
            End If
        ctr1 = ctr1 + 1
        Loop
    End If
End Function
%>
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE