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.

PHP Code:
$htaccess '.htaccess';
$contents file_get_contents($htaccessTRUE
          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(
  if (
in_array($ip$whitelist)) {
  } 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";
          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);