FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Ip Banning Using Php





qqz
Title: IP Banning
Level: Moderate
Time: EST 15 Minutes

I will try to explain this the best i can. First you need to create the database table.
Code:
CREATE TABLE `ipbanning` (

`id` INT NOT NULL,

`ip` VARCHAR(20) NOT NULL,

`banned` ENUM('yes','no') DEFAULT 'no' NOT NULL,

`reason` TEXT NOT NULL

);


Ok now that we have the table set up we need to make it so that when the user visits the site, no matter how many times they visit, it will add their ip to the list and only add it 1 time.

here is the code to do that. I will describe it after

Code:
<?php

$ip = $_SERVER['REMOTE_ADDR'];

$add = mysql_query("SELECT * FROM `ipbanning` WHERE `ip` = '$ip'") or die (mysql_error());

if(mysql_num_rows($add)==0){

mysql_query("INSERT INTO `ipbanning` (`ip`) VALUES ('$ip')") or die (mysql_error());

}else{

}

?>









Ok

$ip = the variable we need and the $_SERVER['REMOTE_ADDR']; is what we need to use to get the ip address.

the the $add is the mysql query that we need to see if the ip address is already in the table

then if(mysql_num_rows($add)==0){ is saying that if a row wasn't could we will add that ip into the database. and it if was we will not.(demonistrated by }else{}



Ok. So we have that done. their ip is in the database. now if they are a returning visitor we need to check and see if they are banned or not.





CODE 



<?php

$row=mysql_fetch_assoc($add);





$reason=$row['reason'];

?>


what that code does is it makes and array out of the mysql_query and checks those fields and then puts whats in the field into the variables.

Now to block them if they are banned.


Code:
<?php

if(($row['banned'])==yes) {

echo "<center>You are banned for the following reason: <br>$reason</center>";

}else{

?>

Ok what that code does is check the banned row and sees if it is equil to yes. and if it is it prints out You are banned for the following reason: then it gives the reason. then the else statement is saying if they aren't banned do this.

then at the end of you page do this

Code:
<?php

}

?>
simplyw00x
So what this really is is a tutorial. Maybe better suited to the tutorials forum?
orno
nice one

btw, for those who blindly copy pasted and found that it didnt work:
in the first CODE section it should be

Code:
`reason` TEXT NOT NULL


not
Code:
`rason` TEXT NOT NULL
izcool
It would be wise to ban hostnames as well, in case if IP Banning isn't the solution.

- Mike.
bluffmango
hi
i m a newbie not only to the forum but the websites stuff i dont have much knowledge abt databases n java stuff but i surely can copy n paste perl scripts n follow instructions to setup......i wanted to know if there is any script which bans an ip which has visted a webpage once for the next 24 hours..??? if there is such a thing a perl one with instructions Wink plzz plz do let me know...........it ll b so nice ov u
Related topics
Can I add tasks to Windows Scheduled Tasks - Using PHP?
[tutor] How to protect images without htaccess using PHP
Using PHP
Includ Images using PHP.
How to start using PHP? Help!
Website Designing using PHP
how can i setup a page break for printer on html using php ?
Reading word documents using PHP?
I am trying to build a contact form using php and flash
Communicating between web pages
how to compress a file using php ?
Checking if ports are up using PHP
Hot to change "Reset Ownership" using php?
Using php to change content of a .txt
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.