开发者

get list of all PHP session_id

Is't possible to get list of all session_id in PHP SESSIONS?

Note: I need maintained some file in the server. One file e开发者_运维问答qual one session. And I need to determine behaviour of old files if SESSION expire.


Thank you all for any advice.


As others have answered, sessions are stored in the path defined by session.save_path in php.ini and you can iterate this directory to retrieve a list of every session.

An alternative approach would be to change the session storage and move it to a database using session_set_save_handler(). You could store all your sessions in a database and do with it whatever you wish.


The best you can do to get a list of all existing session IDs is:

preg_grep("/^sess_/", scandir(ini_get("session.save_path")))

But that won't tell you which files are still valid. And I don't really get the second part of your question. But it is possible to unserialize them manually to probe for attributes.


i'd to do this in an old project.

i named the files with the session_id (something like this: sessionid_some_file.tmp) :

_TMP_PATH_ : path where the files to check are.

foreach(new DirectoryIterator(_TMP_PATH_) as $file)
{
    if(preg_match('/(.*)_(.*)_(.*)\.tmp/', $file->getFilename(), $data))
    {
        session_write_close();
        $session_id = $data[1];
        session_id($session_id);
        session_start();
        if(empty($_SESSION))
        {
            unlink(_TMP_PATH_ . $file->getFilename()); 
        }
    }
}
session_write_close();

foreach files i get the session_id, open it and test if something inside. (i wrote something after each session opened)

But be carefull, this piece of code has never been in a production environnment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜