Page 1 of 1

Reading the servers whole SESSION data

Posted: Fri Nov 02, 2007 8:39 am
by impulse()
I'm looking to create a function that will show all online users. The users are tracked through sessions but I've hit snatch because I'm not sure how I would access every session variable the server has allocated.

Or if there would be another way to do this I'd be grateful for your thoughts.

Regards,

Posted: Fri Nov 02, 2007 3:43 pm
by Apocalypz
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I think the easiest way would be to create a database table to track when a user logs in/out of your system. This would allow you to display the stats you are looking for as well as some other semi-resourceful things such as tracking the most common/popular times for using your system.

However, I think that if you really wanted to search all the sessions, the only way I can think of to do this would be to find the directory that your session files are stored in and count the number of files there. If you properly destroy each session upon a user logging out, this should be a fairly accurate count, unless you initialize a session for users that aren't logged in. If you do this, it's a bit more complicated and possible inefficient. Something like...

Code: Select all

function countLoggedIn()
{
    $files = glob("path_to_files/*"); // glob all files in the session directory

    foreach ($files as $key => $file)
    {
        $file = str_replace("path_to_files","",$file);
        session_id($file);
        session_start();
        if (isset($_SESSION['logged_in']))
        {
            $count++;
        }
        session_destroy();
    }
    return(count);
}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Nov 02, 2007 3:56 pm
by feyd
The file names don't exactly match the session id. They contain the id, sure, but with extra stuff. :)

Parsing the files is generally better than starting and stopping the session. Better yet, store the session data in a database... then you can do it with a simple query.

Posted: Sat Nov 03, 2007 6:18 am
by impulse()
I considered storing a value in a database when they login but I didn't that'd be very accurate because if they closed their browser without logging out then they would indefinitely logged in.

Posted: Sat Nov 03, 2007 8:19 am
by superdezign
Then put an expiration on your sessions. You can easily force sessions to end when you want them to via the database.

Posted: Sat Nov 03, 2007 10:20 am
by impulse()
I've read through a few pages about sessions and I have come up with the following code which doesn't seem to work as expected. I was hoping after 5 seconds of not requesting the page that the value would reset.

Code: Select all

ini_set('session.gc_maxlifetime', 5);

session_start();
++$_SESSION['count'];

echo $_SESSION['count'];
Can you see where I'm going wrong with this?

Posted: Sat Nov 03, 2007 10:45 am
by feyd
The probability is not 100% I would guess. Therefore the session isn't deleted by PHP automatically.

This is why we often recommend switching to database session. They give you more control and you can store information however you wish.

Posted: Sat Nov 03, 2007 10:53 am
by impulse()
I think I agree. I just thought of a simple solution.

On each page insert into a database the username from the session, if set, and a timestamp. Then a page to show online uses calculated by fetching all rows from that database where the timestamp is within the last x seconds. And maybe a CRON to cleanup the database on a daily basis.