Search code examples
zend-framework2zend-session

Zend 2: Check who is logged using session


I'm using Zend\Authentication\AuthenticationService and I would like to know who is logged, but for now I get data only for one user.

For example:

On one browser I log in as John Doe and I get

PHPSESSID = abcdef1

On second browser I log in as Jane Bar and I get

PHPSESSID = abcdef2

Now I would like to get all users who are logged, something like this:

    $storage = new \Zend\Session\Container('Zend_Auth');
    foreach($storage as $data)
    {
        var_dump($data); // John Doe, next Jane Bar
    }

But I can't get anything to work

    $storage = new \Zend\Session\Container('Zend_Auth');
    foreach($storage->getIterator() as $data)
    {
        var_dump($data); // I get only single data depending on which browser I use
    }

or

//$auth = AuthenticationService()
foreach($auth->getStorage()->read() as $data)
{
    var_dump($data); // single data depending on which browser I use
}

or

    foreach($_SESSION['Zend_Auth'] as $session)
    {
        var_dump($session); // single data depending on which browser I use
    }

Any idea how can I get information about all users who are logged in, using session from authentication service?


Solution

  • It doesn't work this way. On each machine, the user's id belongs only to their session. To get a list of logged-in users, you need to keep a track of their latest activity based on timestamps.

    What you really need to do is to a create a separated table with users ids and their last activity, only this way you would know who's online. Then when they do perform some action (like viewing messages), then you update their activity to the current timestamp.

    The table itself might look like this:

    user_id | last_activity
    
       2    | 144454344
       3    | 144445346
    

    Then when you'd want to get a list of currently logged in users, you'd simply select their ids comparing with a timestamp + some seconds. Then knowing ids of users whose timestamp isn't expired, you can query for their entities in another table.

    As a best practice, don't write that logic in controllers. Do your best to keep that in a service object.