Flood control

If you've written your own commenting script or did it following a tutorial, you may have already experienced problems with flood (several comments by the same person). This can lead to problems if the comments are added too fast, since it can slow down the MySQL and even lead to internal server errors.

There's a very simple way to prevent this: we will check if any of the previous comments were done from the same IP and check when they were done, if it's been less than X minutes, it will display a message, if it's been more than X minutes, it will insert the comment normally.

First check if your comments table has an ip field, if it doesn't, you will need to add it. Log in to your PHPMyAdmin and open your comments table, run this query:

ALTER TABLE table ADD ip VARCHAR(20);

Now into the coding. Remember you must combine this with your commenting script.

<?php
// Database connection here

$query="SELECT * FROM table where ip='$ip' and date > CURDATE() - INTERVAL 5 MINUTE ";
$result= mysql_query($query) or die ("Could not execute query : $query." . mysql_error());

if (mysql_num_rows($result) == '0') {

// Insert your comments adding query and everything else here

} else {

echo "You're not allowed to post so soon after your last comment.";

}
?>

Remember to change table with your actual table name and date, if your date field has another name.

If you didn't have an ip field before, you will need to edit your comments adding query and add the IP thing. You will need to edit your comments form too, add this:

<input type="hidden" name="ip" value="<?php $visitorip = $_SERVER['REMOTE_ADDR'];
echo "$visitorip"; ?>" />

That's all. I know this tutorial may be confusing, so feel free to ask at the forum and I'll try to make it clearer.

Note: this method will not work with some bots, because they change IP everytime they make a post, so a solution would be not checking the IP, but only add the comment if the last one (by anyone, from any IP) was added more than X minutes ago.

Tutorial © Melfina. Do not reproduce in any way without previous permission from the author. Printed from http://celestial-star.net