开发者

Re-directing user if they make more than 5 comments in 1 hour

I'm trying to re-direct a user if they try to submit more than 5 comments in 1 hour.

datecommented is a timestamp. I want开发者_如何转开发 the redirect to happen if there are 6 or more comments with loginid = '$uid' having a timestamp in the last hour.

Below is what I have so far, but it does not work. How can I get it to work?

Thanks,

John

$queryuidcount = "select loginid from comment where  (HOUR(NOW()) - HOUR(datecommented)) <= 1 AND loginid = '$uid'"; 
$uidresult = mysql_query($queryuidcount);

if (mysql_num_rows($uidresult) >= 6)
{

   session_write_close();
   header("Location:http://www.domain.com/directory/file.php");
   exit;

}


It's not a good idea to select data when you want to select the number of lines, nor to use HOUR for what you're trying to do. HOUR() just return the hour from a timestamp, and does not care about the day it happened. Plus, with your solution, I could post 5 comments at 10:59 and 5 more at 11:00.
This is the query you should use to know how many comments a user posted in the last hour :

SELECT COUNT(*) as num_comments
  FROM comment
 WHERE datecommented >= NOW() - INTERVAL 1 HOUR
   AND loginid = $uid;

This returns a resultset with one column, num_comments. Then, you can just make verifications in your PHP code and redirect with header, as you're already doing.

You can fetch the row like you usually do with any query in PHP:

// I assume $query contains the query above
$result_set = mysql_query($query) or exit(mysql_error());
$num_comments = mysql_result($result_set, 0); // fetch the first row of the result set

if ($num_comments >= 5) {
    // blablabla
}}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜