用PHP遍历服务器的所有会话 [英] Looping Through All a Server's Sessions in PHP

查看:103
本文介绍了用PHP遍历服务器的所有会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP中是否有一种方法可以获取服务器上所有会话的列表(以及每个会话中的变量)?

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

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

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.

我的PHP非常有限(我通常是.NET开发人员),但是如果有人知道这是否有可能(以及如何做到),我将不胜感激.我在Google上搜索了一下,发现的结果倾向于表明这是不可能的,但是我觉得这很难接受.

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.

还是,如果不能,但是我认为我在StackOverflow上的伙伴可以给我一个明确的答案!

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

推荐答案

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

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 echo "Session Save Path: " . ini_get( 'session.save_path');?>

其内容,然后在浏览器中打开文件.

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

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

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 .

会话数据先进行序列化,然后再存储在磁盘上.这样,存储在会话文件中的对象必须在使用前进行反序列化.但是,如果您使用的是按原样存储的纯文本来存储会话数据(例如$_SESSION['userid'] = 1234)来存储有关用户的信息,那么解析出您要查找的数据就应该足够容易了从文件中获取.

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.

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

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?

调整帖子以符合Itay的评论.

Adjusted post to match Itay's comment.

这篇关于用PHP遍历服务器的所有会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆