PHP会话管理,用Cookie统计用户

发布于 2024-09-05 08:39:56 字数 92 浏览 3 评论 0原文

我想在加载页面时在用户的浏览器中存储一个 cookie。然后,使用 AJAX 计算使用该 cookie 的用户数量,并每隔 1 秒左右将其发送到数据库行。这是如何实现的?

I want to, upon loading the page, store a cookie in the user's browser. Then, using AJAX, count the number of users with that cookie and send it off to a database row every 1 second or so. How is this accomplished?

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

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

发布评论

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

评论(2

猫卆 2024-09-12 08:39:57

由于听起来您正在实现一个聊天室,因此可以安全地假设正在进行一些 AJAXy 轮询(客户端非常频繁地检查更新)。

一种方法可能是在数据库中保留一个 active_sessions 表。 像这样:

create table active_sessions (
   sess_id varchar(32) primary key,
   lastseen timestamp
);

每次客户端请求更新时,插入/更新其会话 ID 的行,然后定期删除时间戳早于 T 的任何记录(对于 T 的某些值,例如 30 秒或其他)

它可能看起来 表中的行可以很好地估计有多少客户端仍然处于活动状态。

Since it sounds like you're implementing a chat room, it's safe to assume there's some AJAXy polling going on (clients are checking for updates very frequently).

One approach might be to keep an active_sessions table in your database. It might look like this:

create table active_sessions (
   sess_id varchar(32) primary key,
   lastseen timestamp
);

Every time a client asks for an update, insert/update the row for their session ID, and then regularly delete any records with a timestamp older that T (for some value of T like 30 seconds or something)

Count the rows in the table to get a pretty good estimate of how many clients are still active.

方圜几里 2024-09-12 08:39:57

看看我的一篇帖子!

在php中的数据库中设置会话

就在您存储数据时向 mysql 添加时间戳并计算时间 > 的唯一行数时间()=30;延迟 30 秒。

Take a look at one of my posts!

set session in database in php

Just when your storing the data add a timestamp to mysql and count the unique rows where the time > time()=30; for a 30 second delay.

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