• Link us
  • Links
  • TOS
  • Site
  • Gallery
  • Forum
Subscribe

Flood control - PHP & MySQL tutorial

Tutorials > PHP & MySQL tutorials > Flood control
  • Added: Jun 21st, 2006
  • Level: Medium
  • Author: Melfina
  • Reads: 12,914
  • Description: Prevent your comments page from being flooded.
Bookmark Digg del.icio.us

E-mail to a friend

View printable version


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.


If you like this tutorial why not digg it or add to del.icio.us?

Tutorial © Melfina. Do not reproduce in any way without previous permission from the author.

  • Members login
  • Register for free

Web layouts

  • Web layouts
  • Aardvark Topsites skins
  • SMF Themes
  • Wordpress Themes

Graphic resources

  • 100x100 Avatars
  • PS and PSP brushes
  • Brush shop
  • Background patterns
  • Web icons and smilies
  • Textures
  • Renders (PNG)

Tutorials

  • Newest tutorials
  • Adobe Photoshop
  • CSS
  • Designs / Layouts
  • HTML & XHTML
  • Miscellaneous
  • Paint Shop Pro
  • PHP & MySQL

Affiliates

  • Ego Box
  • The Jaded Network

Partners

Celestial Star © Melfina 2003-2010. By using this site you agree to our terms of service
Layout credits: Hybrid Genesis, Bittbox, swe3ty
Valid XHTML | Valid CSS | Top of the page