在 PHP 中访问活动会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3426844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:42:23  来源:igfitidea点击:

Access active sessions in PHP

phpsession

提问by G__

How can I get a list of all active PHP sessions on a server and access them from within one user's instance?

如何获取服务器上所有活动 PHP 会话的列表并从一个用户的实例中访问它们?

The motivating case is displaying a list of all currently active users on the site, where usernames are stored in each user's PHP session.

激励案例是显示站点上所有当前活动用户的列表,其中用户名存储在每个用户的 PHP 会话中。

Note: I know that I can create my own state via a database (or even the filesystem), but I'm looking for a way to utilize the built-in PHP session mechanisms.

注意:我知道我可以通过数据库(甚至文件系统)创建自己的状态,但我正在寻找一种利用内置 PHP 会话机制的方法。

采纳答案by Artefacto

Seeing the responses, though it's possible, it doesn't mean you shoulddo it. The format in which the sessions are stored is not documented and may change at any time (even between minor versions).

看到回复,虽然有可能,但这并不意味着你应该这样做。存储会话的格式没有记录,并且可能随时更改(即使在次要版本之间)。

The correctway to do this is to implement your own session handler. It's not that hard, really.

执行此操作的正确方法是实现您自己的会话处理程序。这并不难,真的。

回答by Alastair

Session List

会话列表

<?php
print_r(scandir(session_save_path()));
?>

Check for a Specific Session

检查特定会话

<?php
session_start();
echo (file_exists(session_save_path().'/sess_'.session_id()) ? 1 : 0);
?>

Time Session File Last Changed

上次更改时间会话文件

<?php
session_start();
echo filectime(session_save_path().'/sess_'.session_id());
?>

As has been done to death, already, it isn't best-practice to handle sessions this way but if it's needed, those will work without the need to check/modify the session storage path (useful if you switch to another server with a different configuration).

正如已经死了一样,以这种方式处理会话并不是最佳实践,但是如果需要,它们将无需检查/修改会话存储路径即可工作(如果您切换到另一个服务器不同的配置)。

回答by Abdul Majeed

You cant use sessions to do this, you would have to store the online users in a DB, and just set a timestamp or similar, the display all who are active with in 5 minutes, else delete their records.

你不能使用会话来做到这一点,你必须将在线用户存储在一个数据库中,只需设置一个时间戳或类似的,显示所有在 5 分钟内处于活动状态的人,否则删除他们的记录。

回答by Jim W.

The sessions are stored as files in your temporary session directory. You should be able to locate this in the php.ini (or using phpinfo()). Those are all the sessions. You should be able to check the file time to see if they are active within the last x minutes.

会话作为文件存储在临时会话目录中。您应该能够在 php.ini(或使用phpinfo())中找到它。这些就是所有的会议。您应该能够检查文件时间以查看它们是否在最后 x 分钟内处于活动状态。