What is the best way to restrict someone posting a message every 10 minutes using the Time and Date php functions ? I want it to check whether someone has posted something in the last 10 minutes, if so output an error saying they are not allowed to post.
Any information and code examples would be grea开发者_开发问答t, thanks!
This isn't great if you're going to be using a CDN but it's fast and easy. After someone posts a message drop a timestamp in your session and then check on that the next time they post.
if ($_SESSION['post_time'] && $_SESSION['post_time'] <= strtotime('now -10 minutes')) {
// error
}
$_SESSION['post_time'] = time();
This is open to tampering so it's not perfect by any means. Also, giving a session cookie to anyone that posts is bad for your CDN if you're using one since they usually cannot cache a page for a user that has a cookie. But since you don't really have another way to tracking the user over a 10 minute period it seems to be one of the better options.
I would simply ask database if the user posting has already posted "ten minutes before". Here you will find enough to reach your goal. Another golden link is:
http://www.dreamincode.net/code/snippet86.htm
Hope that helps,
Unix datestamp your database posts using time(). This returns a datestamp to you.
Then you compare current time to the time you had inserted in your database from the user like this:
Assume their user id # is 22:
// returns datestamp equal to ten minutes before the current time
$threshold = time() - 600;
// see if there are any posts by the user
$sql = 'select from post_table where posted_date > '.$threshold.' and uid=22';
Push the $sql string to your query function and if any results happen they already posted within ten minutes ago so you issue an error and bypass the add-record.
精彩评论