I am using the below script to block bot that doesn’t follow robots.txt. IT works when there is not rurl but when some bot has a rurl the script did not block them. I want it to block any IP that goes into this file. Pls help.
[code=php]<?php
$ip = $_SERVER[‘REMOTE_ADDR’];
$htaccess = ‘.htaccess’;
$contents = file_get_contents($htaccess, TRUE)
OR exit(‘Unable to open .htaccess’);
$exists = !stripos($contents, ‘deny from ‘ . $ip . “n”)
OR exit(‘Already banned, nothing to do here.’);
$date = date(‘Y-m-d H:i:s’);
$uri = htmlspecialchars($_SERVER[‘REQUEST_URI’], ENT_QUOTES);
$agent = htmlspecialchars($_SERVER[‘HTTP_USER_AGENT’], ENT_QUOTES);
$agent = str_replace(array(“n”, “r”), ”, $agent);
$email = ”;
$whitelist = array(
);
if (empty($_SERVER[‘HTTP_REFERER’])) {
if (in_array($ip, $whitelist)) {
echo “”;
} else {
$ban = “n# The IP below was banned on $date for trying to access {$uri}n”;
$ban .= “# Agent: {$agent}n”;
$ban .= “Deny from {$ip}n”;
file_put_contents($htaccess, $ban, FILE_APPEND)
OR exit(‘Cannot append rule to .htaccess’);
if (!empty($email)) {
$message = “IP Address: {$ip}n”;
$message .= “Date/Time: {$date}n”;
$message .= “User Agent: {$agent}n”;
$message .= “URL: {$uri}”;
mail($email, ‘Website Auto Ban: ‘ . $ip, $message);
}
header(‘HTTP/1.1 403 Forbidden’, TRUE);
echo ‘<html><head><title>E</title></head><body>
</body></html>’;
}
}
?>