使用 PHP 在一个实例中进行多个会话?

发布于 2024-09-16 09:55:40 字数 346 浏览 5 评论 0原文

我有一个项目,我想在一个浏览器中创建两个会话 cookie。第一个会话是唯一地标识一个人,第二个会话是在用户之间共享会话内的事件。我一直在为此使用数据库,但希望数据在会话终止时消失。系统内没有登录。

除了创建 cookie 系统来复制功能之外,还有其他方法可以做到这一点吗?

例如,我们将有两个会话 Cookie:

name=someRandomUUIDsession=valueSharedBetweenUsers。

我不想与以下人员共享 name 会话多个用户,但 session 会话将是。想法?

I have a project where I would like to create two session cookies in one browser. The first session would be to uniquely identify a person, the second would be to share events within the session between users. I have been using a database for this, but would like the data to disappear when the session dies. There are no logins within the system.

Is there a way to do this, other than creating a cookie system to replicate functionality?

For example, we would have two session cookies:

name=someRandomUUID and session=valueSharedBetweenUsers.

I don't want to share the name session with multiple users, but the session session would be. Thoughts?

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

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

发布评论

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

评论(4

画骨成沙 2024-09-23 09:55:40

如果您想在用户之间共享信息,使用会话并不是最好的主意,因为它使用文件系统。你最好使用处理锁定、并发等所有问题的数据库。

尽管你所要求的在技术上是可能的,但我强烈建议不要这样做。

编辑

假设我正确理解了您的要求,我将这样做:

  1. 仅使用会话来存储与该用户相关的会话数据。它可能包括以下内容:

    $_SESSION['name'] = '测试名称';
    $_SESSION['groupid'] = 2;
    
  2. MySQL 数据库和表,其中包含字段groupidXXXXX(要存储的数据)、timestamp< /p>

每当有人更新特定的信息时组 id,您更新时间戳。

然后运行一个简单的 cronjob 来检查是否有当前时间 - 时间戳 > 3600(一小时),您可以将其视为已过时并删除这些记录。

If you want to share information between users, using a session is not the best idea as it uses the file system. You would be better off using the database which handles all the issues of locking, concurrency etc.

Although what you ask for is technically possibly, I would strongly recommend against it.

EDIT

Assuming I have understood your requirement correctly, here is how I would do it:

  1. Use session only to store session data related to that user. It could include something like:

    $_SESSION['name'] = 'test name';
    $_SESSION['groupid'] = 2;
    
  2. A MySQL DB and table with fields groupid, XXXXX (data you want to store), timestamp

Whenever anyone updates information for a particular group id, you update the timestamp.

Then run a simple cronjob to check if any current time - timestamp > 3600 (one hour) and you can consider that as stale and delete those records.

阿楠 2024-09-23 09:55:40

我*认为*您只能有一个“当前”会话,但您所指的功能是 session_name:

http://www.php.net/manual/en/function.session-name.php

cookie 功能非常简单。我建议改为调查一下。

I *think* you can only have one "current" session, but the functionality you are referring to is session_name:

http://www.php.net/manual/en/function.session-name.php

The cookie functionality is very simple. I suggest looking into that instead.

月下伊人醉 2024-09-23 09:55:40

“valueSharedBetweenUsers”来自哪里?它是常量还是数据库条目?

不管怎样,为每个组创建一个会话是没有意义的。相反,您应该为每个用户提供唯一的会话;将您的“共享”属性作为每个人的会话属性。

因此,启动唯一会话,然后执行

现在每个人都有一个具有唯一 sessionID 和公共值“session”的会话。

(显然,如果您稍后需要更改此属性,则必须为每个经过身份验证的个人单独执行此操作)

Where is the "valueSharedBetweenUsers" coming from? Is it a constant or database entry?

Either way, it wouldn't make sense to create one session per group. You should instead be giving each user a unique session per user; with your "shared" attribute as a session attribute for each individual.

So start the unique session then just do <? $_SESSION['session'] = 'mySharedValue'; ?>

Now everyone has a session with a unique sessionID and a common value 'session'.

(Obviously if you need to change this attribute later you'll have to do it separately for each authed individual)

2024-09-23 09:55:40

这并不像人们让 Facebook 和 Twitter 在用户登录时创建至少 10 个不同的会话那样牵强。

This isnt as far fetched as people are making facebook and twitter have at least 10 different sessions being created when a user has logged in.

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