/    Sign up×
Community /Pin to ProfileBookmark

IP Ban script not fully functional

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>’;

}

}
?>[/code]

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @jsm03 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...