简体   繁体   English

检查用户是否在最近24小时内登录

[英]Checking if the user has logged in the last 24 hours

I'm currently developing a "user rewarding" system in my website to reward the active users with a given virtual currency (points for example) 我目前正在网站上开发“用户奖励”系统,以给定的虚拟货币奖励活跃用户(例如积分)

I'm having a problem thinking of a way to acomplish that. 我在想一种方法来解决这个问题。

I know that I would have to compare timestamps, but I have no idea when I should create the base timestamp, which I would use as the base for my calculations. 我知道我将不得不比较时间戳,但是我不知道何时创建基准时间戳,该基准将用作计算的基准。 I think I may not be explaining my question properly, so I will say it in a short manner: How can I check if the user has logged in the last 24 hours. 我想我可能无法正确解释我的问题,所以我将简短地讲一遍:如何检查用户是否在过去24小时内登录过。 When to create to create the timestamps that I will use for my calculations. 何时创建创建将用于计算的时间戳记。

Thank you in advance for all of your answers. 预先感谢您的所有答复。

I have a last_activity column in my users table 我的users表中有一个last_activity

Well you'd just have to store the timestamps each times your users log in. But if you want to check if they were online no matter if they had to login or not, you'd have implement an update to the timestamp on every page. 好吧,您每次登录时都只需要存储时间戳。但是,如果您想检查他们是否在线,无论他们是否必须登录,都必须在每个页面上实现时间戳的更新。 。 You should also store the last update time in the cookie of the user so that you don't update at each page load but every ten minutes or what not. 您还应该将最后一次更新时间存储在用户的Cookie中,这样就不会在每次页面加载时更新,而是每十分钟更新一次。

Without knowing your application structure, it's hard to be specific, but update a 'last_activity' (or similar) field in your user table on occasion. 不知道您的应用程序结构,很难具体说明,但有时会在用户表中更新“ last_activity”(或类似内容)字段。 Then check if that last_activity value is older than 24 hours. 然后检查last_activity值是否早于24小时。

Add some logic so the last_activity only gets updated every 15 minutes or so, and you won't be doing an extra DB write every page load. 添加一些逻辑,以便last_activity仅每15分钟左右更新一次,并且您不会在每次页面加载时都进行额外的DB写操作。

I'm just going to use some short-hand for this one: 我将为此使用一些简写:

OnRegister (to void giving instant points on first login): OnRegister(在第一次登录时立即给出即时积分):

user->lastPoints = now();

OnLogin: 登录时:

if (user->lastPoints - now > 24h)
{
    if (user->lastPoints - now < 48h)
    {
        user->rewardPoints();
    }
    user->lastPoints = now();
}

Hope this is what you're searching for. 希望这是您要搜索的。

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

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