IP Filter
2002-08-11 10:12:00
Category: perl:cgi
Description: This script is an example of how to deny access to a specific webpage or multiple pages by using the remote user's IP address.
Author: Snap
Viewed: 2793
Rating: (7 votes)


#!/usr/bin/perl
print "Content text:html/text\n\n";
 
##########################################
#                                        #
#   Written by Snap                      #
#           (iam@someoneshouse.com)      #
#                                        #
#   For Metalshell                       #
#       http://www.metalshell.com        #
#                                        #
#  This script is an example of how to   #
#  deny access to a specific webpage or  #
#  multiple pages by using the remote    #
#  user's IP Address.  It matches each   #
#  incomming IP against a list you define#
#  and then allows or denies accordingly #
#                                        #
##########################################
 
 
### This is the list of IP's you want to allow to access this page ###
@allow=("192.168.1.101","192.168.1.102","192.168.1.103","192.168.1.104");
 
### Change the below line to accomidate for the page you want to hide ###
$hidden="hidden.html";
 
 
### Get the Users IP Address ###
$userAddr=$ENV{REMOTE_ADDR};
 
### Set Default to Deny ###
$TestVar="0";
 
### This loop checks the Users IP against the Given List ###
foreach $i (@allow){
 
### If the IP is found in the list, change switch ###
if($i eq $userAddr){
$TestVar="1";
}
 
}#-#-#End Of Foreach
 
###  If the IP address is accepted, open your hidden html file ###
if($TestVar == 1){
open(NEWT,"$hidden");
while(<NEWT>)
{
$HTML=$HTML.$_;
}
close NEWT;
print "$HTML\n";
}
 
### Else, display a deny page (Completely Customizable) ###
elsif($TestVar == 0){
print<<STOP
<html>
<title>Access Denied<\/title>
<body bgcolor=red>
<font size=6 color=black>
<u>Access Denied<\/u>
<\/font>
<br><br>
<b>
Your IP Address $userAddr has been denied Access to this page.<br>
If you feel this is in error, please contact the
<a href=mailto:$ENV{SERVER_ADMIN}>administrator<\/a>.
<\/b>
<\/body>
<\/html>
STOP
}