Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check logged in online users using Zend Framework

Tags:

I want to know how to get the number of users currently online or having active sessions on a website using Zend Framework.

I tried the usual way of reading Session save path, but its not working using Zend. Can anyone here suggest me a good method to know how many active sessions are on the server at any moment of time.

like image 616
Sumit Ghosh Avatar asked Dec 09 '10 00:12

Sumit Ghosh


1 Answers

Recently had that problem. Solved it like this:

Usually a controller is an extension of Zend_Controller_action, for example

class IndexController extends Zend_Controller_Action

What we did in our project was create an extended controller under /library/ME/Controller

class ME_Controller_Base extends Zend_Controller_Action
    public function init()
    {
        parent::init();
    }
}

Using this controller you can extend all your other controllers from it - so, the above default controller goes from

class IndexController extends Zend_Controller_Action

to

class IndexController extends ME_Controller_Base

Important, remember to always call parent::init() in the init() section of your controller (this is good practice anyway)

class IndexController extends ME_Controller_Base
{
    public function init()
    {
        parent::init();
    }
}

Now you can add any code you like to the "Base" controller. As we are using Zend_Auth with a Doctrine user object, the final "base" controller looks like this

class ME_Controller_Base extends Zend_Controller_Action
    public function init()
    {
        parent::init();
        $auth = Zend_Auth::getInstance();
        $this->view->user = $auth;
        $this->user       = $auth;

        // check auth
        ...
        // write an update to say that this user is still alive
        $this->user->getIdentity()->update();
    }
}

The update() method just sets an "updated" field to the current date and flushes the user. You can then just select users who were seen within the last X minutes to show the list.

like image 123
mogoman Avatar answered Sep 22 '22 21:09

mogoman