PDA

View Full Version : ASP IP Page Protect/Block


Tesco
06-11-03, 06:24 AM
Not so long ago, I had to make a page protection script, but not using a username and password, using a IP address - I know, I know, but it's what the client wanted.

Ok, so what it does is simple:
Has a list of IP(s)
Gets the user's IP
If it matches, then continue processing the page
If it doesn't just output a error message.

Heres the code....

<%
Dim EndUser, AuthorisedIP
EndUser = Request.ServerVariables("REMOTE_ADDR")
AuthorisedIP = "xx.xx.xxx.xx"
If EndUser = AuthorisedIP Then
%>
Content of page blah blah.
<% Else %>
Sorry, you do not have access t othis page.
<% End If %>

Of course, change the IP address to suit your needs. Also, if you want multiple address's just declare them at the beginning, and then give them a value.

The script can also be reversed, so you block the people that are listed in your IP list from viewing your site. Heres the code for that.

<%
Dim EndUser, BlockIP
EndUser = Request.ServerVariables("REMOTE_ADDR")
BlockIP = "xx.xx.xxx.xx"
If EndUser = BlockIP Then
%>
Sorry, you have been blocked from this page.
<% Else %>
Page content blah blah.
<% End If %>

Just a note about adding more than one IP to these scripts, just use the same variable. but append a number to it (i.e. BlockIP2, AuthorisedIP2 etc), also, you'll need to add another If for each IP you add.

Don't know if anyone will find this useful but, I just thought I'd add it :)

Stealth
06-11-03, 11:37 AM
or if u wanna be a smart git then you can use an array