views:

53

answers:

3

Basic setup of my site is: user enters a message on the homepage, hits enter and the message is sent though a AJAX request to a file called like.php where it echo's a link that gets sent back to the user.

I have made the input disable when the user presses enter, but there's nothing stopping the user from just constantly flooding like.php with POST request and filling up my database.

Someone here on SO told me to use a token system but didn't mention how. I've seen this being done before and from what I know it is effective.

The only problem I have is how will like.php know it's a valid token? My code is this at the moment:

$token = md5(rand(0, 9999) * 1000000);

and the markup:

<input type="hidden" name="token" value="<?php echo $token ?>" />

Which will send the token to like.php through POST. But how will like.php know that this is a valid token? Should I instead token something that's linked to the user? Like their IP address? Or perhaps token the current minute and check that it's the same minute in like.php...

Any help on this amtter would be greatly appreciated, thanks. :)

+1  A: 

The best way is to use session variables because users can't delete them or modify them as easily as a form element. Or better yet, store the IP address and the time in the database and look it up to see if the user can post again yet.

Aaron Harun
Alright, so if I do use a session what would be the best token to make? I like the idea of hashing their IP address but then that can easily be changed. I wanted to steer away from logging their details in a database because if the site gets a heavy flood there will be a lot of read/writes into the database slowing things down.
VIVA LA NWO
>> because if the site gets a heavy flood there will be a lot of read/writes into the database slowing things down. --- if you're asking so trivia questions - then it is a lot of not-quality-and-not-fast-code and site will be down even without that "unneeded" database read-writes either.
zerkms
The only thing that will really control spamming the form is forcing users to create accounts and confirm the email addresses or a captcha. But even those have their limits.Anything, IP based will be able to be worked around using proxies. Time based ones risk cutting a user off during a post if they take to long typing or are hard to verify. What you can do is use a "once" system where the user is allowed to post one comment per page load. Create a random hash and store it in a database and once it is used up with a post, delete it.No matter the solution, there are ways around it.
Aaron Harun
Also, reading and writing to an optimized database isn't as bad as you think. Many large sites track data in a mysql database, so even if your site gets busy, as long as you are doing more reads than writes, you'll be fine.You can even use delayed inserts to ensure that the important reads take precedence.
Aaron Harun
A: 

Don't reinvent the wheel - just look the last comment time from this user, defined by user_id, user_ip, whatever you have - and decide whether he allowed to post or not.

zerkms
A: 

One minute seems like a length of time that is too long for most users to wait. Tabbed browsing, broadband Internet service and users' tendency to not read every piece of text they're presented all contribute to a mass of users who will most likely get bored after less than a minute of not doing anything.

I would definitely suggest storing the users' IP addresses.

amphetamachine