Forum Moderators: phranque

Message Too Old, No Replies

IP Based Custom 403 Pages

Lots Of Close Solutions...No Specific Solutions

         

ateman

8:36 pm on Dec 5, 2003 (gmt 0)

10+ Year Member



OK, I have been searching for the answer to this for some time and many google searches led to this site with *almost* what I needed, but nothing dead on in terms of a solutions...thats the intro, here is the need...

I know how to restrict users by IP using good old .htaccess and shoot them to a 403 page no problem. What I want to do is to take a certain block of IP's and shoot them to a custom 403 page with some info based on where they are coming from.

For instance, if the ip block of 100.205.*.* belonged to Noname University, I would want all users coming to my site via that ip block to be sent to an error page saying:

"I am sorry, visitors to this site from NoName University have been blocked"

My knowledge in fancy PHP, Perl, CGI, etc is limited to hacking together mostly complete scripts...so the simpler the solution the better....

Big Thanks!

ateman

8:39 pm on Dec 5, 2003 (gmt 0)

10+ Year Member



One detail...I would keep a static, generic, 403 page for all other forbidden traffic that came from outside of that IP block....this custom one would be only for the traffic from within the specified IP

PCInk

9:16 pm on Dec 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's simple PHP/ASP/Perl that you need. Your custom 403 can be a file other than html.

For example in Perl, you could use:

print <beginning_of_doc>;
if($ENV{'IP_ADDRESS} =~ /^100.205./)
{
print "No Name uni have been blocked";
}
else
{
print "You are not permitted to view this page";
}
print <end_of_doc>;

Or alternatively you could redirect:
if($ENV{'IP_ADDRESS'} =~ /^100.205./)
{
print "Location: nonameuniversity.html\n\n";
}
else
{
print "Location: 403.html\n\n";
}

Notes: $ENV{'IP_ADDRESS'} is made up, you'll need to look up what IP_ADDRESS actually is. The '.' is the regular expression actually matches anything, they should really be procedded with a '\', but they'll match the dot anyway. Both are pseudo-code and can be transported to php, asp, perl, c++ etc...

ateman

2:07 pm on Dec 6, 2003 (gmt 0)

10+ Year Member



Thanks a bunch, that is what I am looking for....now where would I put that code?

PCInk

5:37 pm on Dec 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just in a Perl file or php, whatever your comfortable programming in. Then point the 403 error page to the script instead of a html file.