简体   繁体   English

如何在PHP中每小时限制用户的评论/请求

[英]How to limit user's comments/requests per hour in php

I have a site that has few request options (for example add photo, add comment). 我有一个网站,该网站的请求选项很少(例如,添加照片,添加评论)。 I want to limit of requests made by use per certain time, for example so he can't post more than 5 comments within hour, he can't add more than 5 photos per hour etc. 我想限制每隔一定时间使用所发出的请求,例如,这样他就不能在一小时内发布超过5条评论,每小时不能添加5张以上的照片,等等。

My idea was to make/update session variable every time form action is sent, so it sums up to 5 (and if session var == 5 it would deny action on every form). 我的想法是每次发送表单操作时都要进行会话变量的创建/更新,因此它总计为5(如果会话var == 5,它将拒绝对每个表单进行操作)。 My idea seems good in my mind, but i just can't find the way to reset certain session variable 1 hour from it's initation). 我的想法在我看来似乎不错,但我只是找不到从启动1小时就重置某些会话变量的方法。 Looking forward for any ideas 期待任何想法

Do it from SQL using simple SQL commands you can get the number of items done in the past hour and thus no need to use session variables (which will die if a user reset it's session) 使用简单的SQL命令从SQL执行此操作,您可以获得过去一小时内完成的项目数,因此无需使用会话变量(如果用户重置会话,该变量将消失)

Check the number of "posts" for a specific element in the current hour 检查当前小时中特定元素的“帖子”数量

SELECT
    COUNT(*) 
FROM 
    my_elements_table 
WHERE 
    HOUR(createdon) = HOUR(NOW()) 
    AND DATE(createdon) = CURDATE()
    AND createdby = the_user_you_are_checking

Check the number of "posts" for a specific element in the past hour 检查过去一个小时中特定元素的“帖子”数量

SELECT
    COUNT(*) 
FROM 
    my_elements_table 
WHERE 
    DATE_ADD(createdon, INTERVAL 1 HOUR) > NOW() 
    AND createdby = the_user_you_are_checking

Obviously, adapt the SQL based on your database fields and tables but you should have a good starting point with that. 显然,根据您的数据库字段和表来修改SQL,但是您应该以此为起点。

I guess you store data about the comments and the photos in a database, at least you have to do it about the comments, but I guess you do it for the photos as well. 我猜您将有关评论和照片的数据存储在数据库中,至少您必须对评论进行处理,但是我想您也对照片进行处理。 In that case I would save a timestamp for when the comment/photo was created and an ID of the user who created it, along with the rest of the information. 在这种情况下,我将保存创建注释/照片的时间戳以及创建注释/照片的用户的ID,以及其他信息。

When a user then tries to add another photo or comment, you count the number of comments and photos in the db that were created by that particular user within the last 60 minutes. 然后,当用户尝试添加另一张照片或评论时,您可以计算该特定用户在最近60分钟内创建的评论和照片的数量。 If it exceeds five, you discard the request, otherwise you add the information. 如果超过五个,则放弃请求,否则添加信息。

Well if you got users, you store them in a database, don't you ? 好吧,如果您有用户,可以将它们存储在数据库中,不是吗? Then why not just store the last time they commented something in the database and use that to check if they can comment ? 那么,为什么不仅仅将他们上次评论的内容存储在数据库中,并使用它们来检查是否可以发表评论呢?

在会话中设置5个变量,其中包含操作时间,并且每次用户尝试发布时都要先检查以查看是否所有5个对象都已记录并且是否所有数据都具有记录时间,如果所有时间都在一小时内从当前时间开始,然后拒绝访问。

Another solution would be to query your database to return comments posted within your specified time frame and if the result count is higher than allowed, don't allow new comment. 另一个解决方案是查询数据库以返回在指定时间范围内发布的评论,如果结果计数高于允许的范围,则不允许新评论。

Something like: "SELECT created_on FROM tblComments WHERE user_id= x " 类似于:“从tblComments SELECT WHERE user_id = x中选择created_on”

With this I am making an assumption that you are storing comments in a database and that you have a field containing the post time. 以此为前提,我假设您将注释存储在数据库中,并且具有包含发布时间的字段。

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

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