ASP - Only allow access via specific IP address

Discussion in 'ASP General' started by Jez, Oct 27, 2008.

  1. Jez

    Jez Guest

    I'm trying to work this out..

    But I'm a little stuck...

    <%
    'declare variables
    Dim sIP1
    Dim sIP2
    Dim sIP3

    'assign our APPROVED IP addresses (all extrenal addresses)
    sIP1 = "xxx.xxx.xxx.xxx"
    sIP2 = "xxx.xxx.xxx.xxx"
    sIP3 = "xxx.xxx.xxx.xxx"

    'retrieve the visitors IP address
    sIP = Request.ServerVariables("REMOTE_ADDR")

    'check if IP address matches any of the approved IPs
    response.write sIP
    If sIP <> sIP1 OR sIP <> sIP2 OR sIP <> sIP3 Then
    Response.Redirect "http://www.google.com"
    else
    response.write "HELLO & WELCOME"
    End If
    %>

    How do I do the IF statement ?? if sIP is anything other that sIP1 or
    sIP2 or sIP3 then ???
    Thanks
     
    Jez, Oct 27, 2008
    #1
    1. Advertising

  2. Jez wrote:
    > I'm trying to work this out..
    >
    > But I'm a little stuck...
    >
    > <%
    > 'declare variables
    > Dim sIP1
    > Dim sIP2
    > Dim sIP3
    >
    > 'assign our APPROVED IP addresses (all extrenal addresses)
    > sIP1 = "xxx.xxx.xxx.xxx"
    > sIP2 = "xxx.xxx.xxx.xxx"
    > sIP3 = "xxx.xxx.xxx.xxx"
    >
    > 'retrieve the visitors IP address
    > sIP = Request.ServerVariables("REMOTE_ADDR")
    >
    > 'check if IP address matches any of the approved IPs
    > response.write sIP
    > If sIP <> sIP1 OR sIP <> sIP2 OR sIP <> sIP3 Then
    > Response.Redirect "http://www.google.com"
    > else
    > response.write "HELLO & WELCOME"
    > End If
    > %>
    >
    > How do I do the IF statement ?? if sIP is anything other that sIP1 or
    > sIP2 or sIP3 then ???
    > Thanks


    OR - returns true if one of the logical expressions is true. sIP cannot be
    equal to all three approved addresses at the same time can it? No. Which
    means the OR expression will always return true, won't it? You need to use
    AND instead of OR if you want to use inequality tests.
    If sIP <> sIP1 AND sIP <> sIP2 AND sIP <> sIP3 Then
    This expression will only return True if all three of the inequalities are
    true.

    Alternatively, you could turn it around:
    If sIP = sIP1 OR sIP = sIP2 OR sIP = sIP3 Then
    response.write "HELLO & WELCOME"
    else
    Response.Redirect "http://www.google.com"
    End If

    Or use Select Case, or InStr():

    Dim s:s="," & sIP1 & "," & sIP2 & "," & sIP3 & ","
    If Instr(s, "," & sIP & ",")>0 then

    response.write "HELLO & WELCOME"
    else
    Response.Redirect "http://www.google.com"
    End If

    --
    Microsoft MVP - ASP/ASP.NET - 2004-2007
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
     
    Bob Barrows [MVP], Oct 27, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carlos
    Replies:
    4
    Views:
    11,701
    Alan Samet
    Sep 22, 2005
  2. =?Utf-8?B?Y2FzaGRlc2ttYWM=?=

    Allow anonymous access only

    =?Utf-8?B?Y2FzaGRlc2ttYWM=?=, Mar 31, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    6,808
    Teemu Keiski
    Apr 3, 2006
  3. Anan
    Replies:
    8
    Views:
    15,896
    John C. Bollinger
    Dec 8, 2004
  4. Jeff
    Replies:
    2
    Views:
    987
    clintonG
    Sep 19, 2006
  5. Ryan Taylor
    Replies:
    1
    Views:
    718
    Ryan Taylor
    Sep 9, 2004
Loading...

Share This Page