><?php
/* Put the following into your .htaccess file.
* If you already have order and deny lines,
* simply add a new deny line for badip.
* -----------------------------------------------
## Prevent web clients from accessing any .ht* file.
<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>
##Start Bad IPs - The script uses the End line, so
## DO NOT ALTER IT
##End Bad IPs
order deny,allow
deny from env=badip
* -----------------------------------------------
* Save this file under any name that only hackers will
* access.
*/
// Note that this is the .htaccess file in the same directory.
// You can place it in a parent directory to block access to
// subdirectories other than this one. That means you'll
// have to put in the correct path for $file.
// Make sure permissions on .htaccess allow it to be written.
// ---------------------------------------------------------------
$file = "./.htaccess";
$fp = fopen($file, rb) or die;
// Read the file.
flock($fp, LOCK_SH);
$data = fread($fp, filesize($file));
flock($fp, LOCK_UN);
fclose($fp);
// Modify $data here.
// Add new Bad IP above the line ##End Bad IPs
// Note this code will add the bad IP above every occurrence of that line.
$badIp = $_SERVER['REMOTE_ADDR'];
$data = preg_replace( '/^##End Bad IPs$/m',
"SetEnvIf REMOTE_ADDR \"^$badIp\$\" badip
##End Bad IPs", $data );
echo "<br /><br />";
echo "New file:<br />$data";
// Write the modified file.
$fp = fopen($file, wb) or die;
flock($fp, LOCK_EX);
fwrite($fp, $data);
flock($fp, LOCK_UN);
fclose($fp);
?>
-Dave Scotese.