Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

Access active sessions in PHP

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

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...