Access active sessions in PHP

后端 未结 4 830
长发绾君心
长发绾君心 2020-11-28 13:52

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 current

相关标签:
4条回答
  • 2020-11-28 14:12

    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.

    0 讨论(0)
  • 2020-11-28 14:14

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

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

    0 讨论(0)
  • 2020-11-28 14:17

    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.

    0 讨论(0)
  • 2020-11-28 14:26

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

    0 讨论(0)
提交回复
热议问题