This script can be used to ban an IP Address from any function on your site. While a .htaccess file can be used to ban, it cannot be selective by function. The script will also ban based on partial IP Addresses. In this example the banned IP Addresses are in a file called IP.dat (in the same directory as the script) with one address per line.
- - Start Script Here - -
- - End Script Here - -
- - Start Script Here - -
<?php
$IP = $_SERVER['REMOTE_ADDR'];$IP_array = file('IP.dat');$IP_array = str_replace("\n", "", $IP_array);$IP_array = str_replace("\r", "", $IP_array);
foreach ($IP_array as $IP_test) {
if (substr_count($IP, $IP_test) != "0") {
echo "<script type='text/javascript'>
alert ('Your IP Address has been banned from the Member's Only Area.');
self.location.replace('banned.htm');
</script>";
die();
}
}?>- - End Script Here - -
Using subdomains with multiSite
If you have a mutliSite account (unlimited domains) at site5.com, the default behaviour for subdomains is not intuitive. If you enter a subdomain that is not specifically defined in the DNS it will default to the primary domain name, even with the wildcard spec in the DNS. You can fix this using the .htaccess code below in your primary domain. It will strip off the subdomain and re-route the request back to the proper domain name. If you do not do this, subdomains will not work properly.
RewriteEngine On
RewriteCond %{HTTP_HOST} !primarydomain.com$
RewriteCond %{HTTP_HOST} !^999.999.999.999
RewriteCond %{HTTP_HOST} (www\.)?([^.]+\.)(.*)$
RewriteRule ^(.*)$ http://%3/$1 [L]
Change primarydomain.com to your primary domain name and use your IP Address.
Or . . . if you would prefer to just give an error (custom 404) page when an undefined subdomain is entered you can use this htaccess code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !primarydomain.com$
RewriteCond %{HTTP_HOST} !^999.999.999.999
RewriteRule ^(.*)$ 404.php [L]
You can use any custom error page, in this example named 404.php, (you will find one on this Tips site) or make your own.
RewriteEngine On
RewriteCond %{HTTP_HOST} !primarydomain.com$
RewriteCond %{HTTP_HOST} !^999.999.999.999
RewriteCond %{HTTP_HOST} (www\.)?([^.]+\.)(.*)$
RewriteRule ^(.*)$ http://%3/$1 [L]
Change primarydomain.com to your primary domain name and use your IP Address.
Or . . . if you would prefer to just give an error (custom 404) page when an undefined subdomain is entered you can use this htaccess code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !primarydomain.com$
RewriteCond %{HTTP_HOST} !^999.999.999.999
RewriteRule ^(.*)$ 404.php [L]
You can use any custom error page, in this example named 404.php, (you will find one on this Tips site) or make your own.
Comments
Post a Comment