简体   繁体   English

如何限制用户每天可以发布的评论或回复评论的数量

[英]how to limit the amount of comments or replies to comments a user can post per day

I have a comment section and a reply to comment section on my social.network.我的 social.network 上有评论部分和评论部分的回复。 We are having some trouble with manual spammers, and I was going to limit the amount of comments someone could post a day.我们在手动垃圾邮件发送者方面遇到了一些麻烦,我打算限制某人一天可以发布的评论数量。

Here are the insert queries for comments and reply to comments:以下是评论和回复评论的插入查询:

//COMMENTS

$query = "INSERT INTO `CysticAirwaves` ( 
                                        `FromUserID`,
                                        `ToUserID`,
                                        `comment`,
                                        `status`,
                                        `statusCommentAirwave`,
                                        `date`,
                                        `time`

                                ) VALUES (

                                    '" . $auth->id ."',
                                    '" . $prof->id ."',
                                    '" . mysql_real_escape_string($_POST['ProfileComment']) ."',
                                    'active',
                                    'active',
                                    '" . date("Y-m-d") . "',
                                    '" . date("G:i:s") . "')";
    mysql_query($query,$connection); 

    if($auth->id == $prof->id) {
        $just_inserted = mysql_insert_id();
        $query = "UPDATE `CysticAirwaves` SET `status` = 'dead' WHERE `FromUserID` = '" . $auth->id . "' AND `ToUserID` = '" . $prof->id . "' AND `id` != '" . $just_inserted . "'";
        $request = mysql_query($query,$connection);
}

//REPLIES

$query = "INSERT INTO `CysticAirwaves_replies` (
                                    `AirwaveID`,
                                    `FromUserID`,
                                    `comment`,
                                    `status`,
                                    `date`,
                                    `time`
                                ) VALUES (
                                    '" . mysql_real_escape_string($_POST['comment']) . "',
                                    '" . $auth->id . "',
                                    '" . mysql_real_escape_string($_POST['reply']) . "',
                                    'active',
                                    '" . date("Y-m-d") . "',
                                    '" . date("G:i:s") . "'
                                    )";
    mysql_query($query,$connection);

    $mailto = array();

    /* get the person that wrote the inital comment */
    $query = "SELECT `FromUserID` FROM `CysticAirwaves` WHERE `id` = '" . mysql_real_escape_string($_POST['comment']) . "' LIMIT 1";
    $request = mysql_query($query,$connection);
    $result = mysql_fetch_array($request);
    $comment_author = new User($result['FromUserID']);

thanks in advance提前致谢

You can perform a select to see how many entries are in the table already by that user for the current date:您可以执行 select 以查看该用户在当前日期的表中已有多少条目:

SELECT COUNT(*)
FROM   CysticAirwaves
WHERE  userid = $auth->id
  AND  date = CURDATE()

Then only perform the INSERT if the number is below your threshold.然后仅在数字低于您的阈值时才执行INSERT Alternatively, you can place a trigger on the INSERT that does this check with every INSERT and bounces the call as well.或者,您可以在INSERT上放置一个触发器,它对每个INSERT执行此检查并弹回调用。 ("Best practice" would be to place it in the database as this would be a database-related limitation, but that's your call) (“最佳实践”是将它放在数据库中,因为这是与数据库相关的限制,但这是你的决定)

It's been a while since I've done MySQL triggers, but I think think is what you're after:自从我完成 MySQL 触发器以来已经有一段时间了,但我认为这就是你所追求的:

delimeter |

CREATE TRIGGER reply_threshold BEFORE INSERT ON CysticAirwaves_replies
  FOR EACH ROW BEGIN
    DECLARE reply_count INT;
    SET reply_count = (SELECT COUNT(*) FROM CysticAirwaves_replies WHERE userid = NEW.userid AND `date` = CURDATE());
    IF reply_count > 5 THEN
      SIGNAL SQLSTATE SET MESSAGE_TEXT = 'Too many replies for today';
    END IF;
  END;

|
delimeter ;

Essentially, if you go to insert a reply in the table and the threshold has been exceeded, a sql error will be raised stopping the action.本质上,如果您 go 在表中插入回复并且已超过阈值,则会引发 sql 错误以停止操作。 You can't "prevent" an insert per-say, but you can raise an exception that makes it fall-through.你不能“阻止”每次插入,但你可以引发一个异常,让它失败。

You can only limit this by the ip address when you don't have a login system.当您没有登录系统时,您只能通过 ip 地址进行限制。 But the ip can change and this is here the problem.但是 ip 可以改变,这就是问题所在。

The best way is to secure the form by a login.最好的方法是通过登录来保护表单。 That only user can post when they are logged in.只有用户在登录后才能发帖。

Last technique is to use a captcha like Recaptcha then at most time bots fill out your form and spam to your system.最后一种技术是使用像Recaptcha这样的验证码,然后大多数时候机器人会填写您的表格并向您的系统发送垃圾邮件。

When you have a login.当您登录时。 Then make a table related to your usertable and count the INSERTS.然后制作一个与您的用户表相关的表并计算插入数。 Before you INSERT a new comment check the table if there was a INSERT today.在您插入新评论之前,请检查表格今天是否有插入。

Before to insert the comment, you check if the user has posted more than 5 comments in the day.在插入评论之前,您检查用户当天发表的评论是否超过 5 条。 If yes, you don't insert the comment and you display a message.如果是,则不插入评论并显示一条消息。

SELECT COUNT(*) FROM CysticAirwaves_replies WHERE FromUserID = the_user_id AND date = CURDATE()

Besides counting before each insertion, you can store the number of comments made by an user somewhere directly, so you don't have to do the count(*) every time (which can be expensive if an user has lots of comments and the table you have is somewhat big).除了在每次插入之前进行计数外,您还可以直接将用户发表的评论数存储在某个地方,这样您就不必每次都进行计数(*)(如果用户有很多评论和表格,这可能会很昂贵你有点大)。

Like, on commenting:比如,评论:

SELECT comment_count FROM comment_count_table WHERE user_id = ?

If that value is small enough, you do:如果该值足够小,您可以:

UPDATE comment_count_table SET comment_count = comment_count + 1 WHERE user_id = ?

Be careful with this since you'd need to reset that counter somehow.小心这一点,因为您需要以某种方式重置该计数器。 At my company we implemented this setting a "last modified field".在我的公司,我们将此设置实现为“最后修改的字段”。 When we do the SELECT, if the "last modified day" is not today, then we reset the counter.当我们执行 SELECT 时,如果“最后修改日期”不是今天,那么我们会重置计数器。

Another option is to have a cron job that resets the counter for all users once every day, but that is way too expensive.另一种选择是有一个 cron 作业,每天为所有用户重置一次计数器,但这太昂贵了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM