ASP - Only allow access via specific IP address

J

Jez

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
 
B

Bob Barrows [MVP]

Jez said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top