在 PHP 中循环遍历服务器的所有会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/675913/
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-24 23:26:54  来源:igfitidea点击:

Looping Through All a Server's Sessions in PHP

phpsession

提问by Ash

Is there a way in PHP to get a list of all sessions (and the variables within each) on the server?

PHP 有没有办法获取服务器上所有会话(以及每个会话中的变量)的列表?

Basically, we have a maintenance function which needs to know which users are currently logged into the site. We already store data for each user in a session variable, but I am hoping that I can loop through each of these sessions and pluck out the data I need.

基本上,我们有一个维护功能,需要知道哪些用户当前登录到站点。我们已经将每个用户的数据存储在一个会话变量中,但我希望我可以遍历每个会话并提取我需要的数据。

MY PHP is very limited (I am a .Net developer ussually) but if anyone knows if this is even possible (and how to do it) I'd be very grateful. I googled this, and the results I found tended to inidcate that it WASN'T possible, but I find this very hard to accept.

我的 PHP 非常有限(我通常是 .Net 开发人员)但如果有人知道这是否可行(以及如何做到),我将不胜感激。我用谷歌搜索了这个,我发现的结果往往表明这是不可能的,但我发现这很难接受。

Still, If you can't you can't but I thought my buddies on StackOverflow could give me a definitive answer!

尽管如此,如果你不能你不能,但我认为我在 StackOverflow 上的朋友可以给我一个明确的答案!

采纳答案by cmptrgeekken

PHP stores session data for each user in a temporary folder on the server. This folder is defined in the php.ini configuration file under the variable session.save_path. Locate this value from within your php.ini file, or alternatively, create a php file with:

PHP 将每个用户的会话数据存储在服务器上的一个临时文件夹中。该文件夹定义在 php.ini 配置文件中的 session.save_path 变量下。从您的 php.ini 文件中找到此值,或者,创建一个 php 文件:

<?php echo "Session Save Path: " . ini_get( 'session.save_path');?>

as it's contents, and open the file in your browser.

作为它的内容,然后在浏览器中打开文件。

Once you find the save path for the session data, open up that folder and you'll notice a fairly simple structure. All sessions are stored in the format: sess_$SESSIONID .

找到会话数据的保存路径后,打开该文件夹,您会注意到一个相当简单的结构。所有会话都以以下格式存储: sess_$SESSIONID 。

Session data is serialized before being stored on disk. As such, objects stored in the session file would have to be deserialized before being usable. However, if you're using plain text, which is stored as-is, to store your session data (ex. $_SESSION['userid'] = 1234) to store information about your users, it should be easy enough to parse out the data you're looking for from within the files.

会话数据在存储到磁盘之前被序列化。因此,存储在会话文件中的对象在可用之前必须反序列化。但是,如果您使用按原样存储的纯文本来存储会话数据(例如$_SESSION['userid'] = 1234)以存储有关用户的信息,那么从内部解析出您要查找的数据应该很容易文件。

One more thing ... I haven't looked into it, but it appears as though the session ID that appears in the filename corresponds directly to, for instance, the name of the PHPSESSID cookie stored on the user's computer. So, with this in mind, it may be possible to loop through the files within the temporary session directory, acquire all the $SESSIONID values, set the current session ID using session_id($SESSIONID), start a session with session_start()and access the data you need through PHP without having to parse the contents files themselves. Can anyone confirm whether or not this would be possible?

还有一件事......我还没有研究它,但看起来好像文件名中出现的会话 ID 直接对应于,例如,存储在用户计算机上的 PHPSESSID cookie 的名称。因此,考虑到这一点,可能会遍历临时会话目录中的文件,获取所有 $SESSIONID 值,使用 设置当前会话 ID session_id($SESSIONID),启动会话session_start()并通过 PHP 访问您需要的数据,而无需解析内容文件本身。谁能确认这是否可行?

Edit:Adjusted post to match Itay's comment.

编辑:调整后的帖子以匹配 Itay 的评论。

回答by Alastair

Here's a more succinct way to get a list of sessions via the stored files:

这是通过存储的文件获取会话列表的更简洁的方法:

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

回答by mgroat

This will get you the data for all sessions, stored in an array and indexed by session id:

这将为您获取所有会话的数据,存储在数组中并按会话 ID 索引:

<?php
$allSessions = [];
$sessionNames = scandir(session_save_path());

foreach($sessionNames as $sessionName) {
    $sessionName = str_replace("sess_","",$sessionName);
    if(strpos($sessionName,".") === false) { //This skips temp files that aren't sessions
        session_id($sessionName);
        session_start();
        $allSessions[$sessionName] = $_SESSION;
        session_abort();
    }
}
print_r($allSessions);

回答by Wooya

I used the @mgroat method in the ajax call, but there is a problem in the header of the HTTP response that the Set-Cookie header appears multiple times and jQuery reports an error:

我在ajax调用中使用了@mgroat方法,但是HTTP响应的头部出现了一个问题,Set-Cookie头部出现多次,jQuery报错:

Set-Cookie header is ignored in response from url: mysite.com/admin/ajax/main_ajax. Cookie length should be less than or equal to 4096 characters.

响应来自 url: mysite.com/admin/ajax/main_ajax 的 Set-Cookie 标头被忽略。Cookie 长度应小于或等于 4096 个字符。

The solution is to add header_remove("Set-Cookie")right after session_start().

解决方案是header_remove("Set-Cookie")session_start().