cron - 如果会话过期则更新数据库

发布于 2024-11-24 19:20:09 字数 250 浏览 0 评论 0原文

我在 mysql 数据库中有一个“用户”表,如果用户在线,我会在一个字段中保留信息。

一切正常,除了:

- 如果某个用户关闭浏览器,他的在线状态不会更改为离线。

有人告诉我可以用 cron 来做到这一点,但我对此一无所知。

我可以将用户 ID 保存在他的会话中,并使用 php 函数来更新数据库。

但是...我不知道如何让 cron 运行该 php(使用会话中的 id)

做起来有多容易?

I have got a "users" table in mysql database, in one field I keep the info if the user is online.

Everything works fine apart of:

-if some user closes the browser, his online status doesn't change to offline.

I have been told that I could do that with cron, but I don't know anything about it.

I can save the user id in his session and make the php function to update the db.

But... I have no idea how to make the cron run that php (with the id from the session)

How easy is it to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

[浮城] 2024-12-01 19:20:09

这相当简单。这可能位于您的 PHP 文件中(称为 update.php)。

<?php
    /*connect to mysql as per normal*/
    // log users off (hopefully, you're using enum here, but for an example...)
    mysql_query( 'UPDATE USERS SET STATUS = \'LOGGED_OFF\' '.
               // whose last action was more than 14400 seconds ago (4 hours)
               'WHERE (CURRENT_TIMESTAMP() - TIMESTAMP(LAST_ACTION)) > 14400' ); 
?>

然后,打开 crontab:

crontab -e

并添加此行以每 15 分钟运行一次(如果您愿意,请将数字 15 更改为另一个数字):

*/15 * * * * /path/to/update.php

It is fairly simple. This might be in your PHP file (call it update.php).

<?php
    /*connect to mysql as per normal*/
    // log users off (hopefully, you're using enum here, but for an example...)
    mysql_query( 'UPDATE USERS SET STATUS = \'LOGGED_OFF\' '.
               // whose last action was more than 14400 seconds ago (4 hours)
               'WHERE (CURRENT_TIMESTAMP() - TIMESTAMP(LAST_ACTION)) > 14400' ); 
?>

Then, open crontab:

crontab -e

and add this line to run the tast every 15 minutes (change the number 15 to another number if you like):

*/15 * * * * /path/to/update.php

旧人 2024-12-01 19:20:09

如果您可以根据请求将用户的活动时间戳与当前时间进行比较,为什么还要使用 cron?

我假设只要用户一段时间不活动,状态就会变为离线。因此,您必须将上次活动的时间存储在数据库中。因此,当您需要响应用户的状态时,请将此时间与当前时间进行比较,如果差异超过允许范围,则返回offline

Why use cron if you can just compare user's activity timestamp with current time when requested?

I assume that status goes to offline whenever user was not active for some time. So you must be storing the time of last activity in db. So when you need to respond witb user's status, compare this time with current and return offline if diffeence is more than allowance.

半世蒼涼 2024-12-01 19:20:09

大多数在线系统都有 15 分钟的检查时间。每次有人做某事时,您都会设置一个时间戳,那么如果他们离线,他们不会更新时间戳,而只是将当前时间戳与数据库中的时间戳进行比较,如果超过 15 分钟,则删除。

Most online systems have a 15 minute check period. You set a timestamp everytime someone does something, then if they go offline they would not update their timestamp and simply compare current timestamp to that in the db and if its more then 15 minutes, delete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文