/    Sign up×
Community /Pin to ProfileBookmark

Ip ban or something like that

Hello, i need script what ban or prohibit IP for 10 minutes. It can be over MySql too. Please help :rolleyes:
Thank you!

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@SyCoJun 03.2009 — This function from here claims to get the real IP address of a user.

[code=php]function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}[/code]


Once you have it you'll need to store it along with the time the ban starts in a SQL table. You then need a monitoring script that runs every minute. You can use a cronjob on Unix or a scheduled task on Windows so the job/task calls a script that looks in the table to see is any bans started more than 10 minutes ago and if so remove that record from the database.

The links provided should give you all the tutorials you need but post back f you need help with any of it.

If you'd like someone to write it for you this site can help.
Copy linkTweet thisAlerts:
@STREETRULEZJun 03.2009 — a script wich bans an user for 10 minutes is commin' up

if your server don't know the $_SERVER['REMOTE_ADDR'] try the script from SyCo to get the IP address, but i don't think, that this var causes any trouble, cause APACHE and IIS are the most used web servers =)

and there we go:

first, we have to create the dbase-table:

it is not nessasery, witch engine you use, only the fields must exist:

4 mysql:
[CODE]
CREATE TABLE ipban.ipban (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
ipaddress VARCHAR( 15 ) NOT NULL ,
timestamp TIMESTAMP NOT NULL ,
bantime TIMESTAMP NOT NULL ,
reason VARCHAR( 255 ) NOT NULL
) ENGINE = MYISAM
[/CODE]


and the PHP-Code

(watch out for the first to lines in code and fit it to your server)

[code=php]
<?PHP
/**
* STREETRULEZ
**/

mysql_connect("localhost", "root", ""); // connect to a server
mysql_select_db("ipban"); // select the database

ipban_banip($_SERVER['REMOTE_ADDR']); // this will ban the ipadress

/**
* returns false, if ip is not banned
* else returns the ip and reason for banning
* su&#252;pporting a die-mode XD
**/
function ipban_checkip($ip, $die = false)
{
$res = mysql_query("select * from ipban where ipaddress = '". $ip ."' LIMIT 1;");
if(mysql_num_rows($res) < 1) return false;
$r = mysql_fetch_array($res, MYSQL_ASSOC);
if($die)
die($r['ipaddress'] ." is banned because ". $r['reason']);
else
return $r['ipaddress'] ." is banned because ". $r['reason'];
}

/**
* add ip vor time secs with the reason
* to the bantable
**/
function ipban_banip($ip, $time = 600, $reason = "you are banned!")
{
$btime = time() + $time;
$btime = date("Y-m-d H:i:s", $btime);
mysql_query("insert into ipban (ipaddress, timestamp, bantime, reason) values ('". $ip ."', NOW(), '". $btime ."', '". $reason ."')") or die(mysql_error());
return true;
}
?>
[/code]


finished XD

this is simple code =)

i've wrote that down in 10 minutes using notepad++ and XAMP from apachefriends.org XD

have phun with it - if it causes troubles feel free to query me some questions by pm or mail [email][email protected][/email]


sorry, bout bad english - i'm from austria and my mothers tounge is german -.-


so have a nice day,

maxx
Copy linkTweet thisAlerts:
@SyCoJun 03.2009 — You'll still need a monitoring script that unbans the ip after 10 mins. The snippet I posted is to get the real ip if it's behind a proxy. Some ISPs put all their customers behind a handful of IPs so by banning the ip of the proxy you ban hundreds or thousands of people.
Copy linkTweet thisAlerts:
@STREETRULEZJun 04.2009 — hi there =)

i'm sorry to forget 'bout that... i was stressed cause my daughter was born 2 days ago =)

just rewrite the first function, and there it goes:

(the whole script ^^)

[code=php]
<?PHP
/**
* STREETRULEZ
**/

mysql_connect("localhost", "root", "");
mysql_select_db("ipban");

//ipban_banip($_SERVER['REMOTE_ADDR'], 600);
//ipban_checkip($_SERVER['REMOTE_ADDR'], 600, true);

/**
* returns false, if ip is not banned
* else returns the ip and reason for banning
* supporting a die-mode XD
**/
function ipban_checkip($ip, $time = 600, $die = false)
{
$btime = time() + $time;
$btime = date("Y-m-d H:i:s", $btime);
mysql_query("delete from ipban where bantime > '". $btime ."';");
$res = mysql_query("select * from ipban where ipaddress = '". $ip ."' LIMIT 1;");
if(mysql_num_rows($res) < 1) return false;
$r = mysql_fetch_array($res, MYSQL_ASSOC);
if($die)
die($r['ipaddress'] ." is banned because ". $r['reason']);
else
return $r['ipaddress'] ." is banned because ". $r['reason'];
}

/**
* add ip vor time secs with the reason
* to the bantable
**/
function ipban_banip($ip, $time = 600, $reason = "you are banned!")
{
$btime = time() + $time;
$btime = date("Y-m-d H:i:s", $btime);
mysql_query("insert into ipban (ipaddress, timestamp, bantime, reason) values ('". $ip ."', NOW(), '". $btime ."', '". $reason ."')") or die(mysql_error());
return true;
}
?>
[/code]
Copy linkTweet thisAlerts:
@SyCoJun 04.2009 — 
i'm sorry to forget 'bout that... i was stressed cause my daughter was born 2 days ago =)[/QUOTE]


Congratulations! Now shut down the PC and go enjoy you little one ?
×

Success!

Help @Mart94 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 6.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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...