Question

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.

Was it helpful?

Solution

You can't use sessions to do this, you would have to store the online users in a DB and display all who are active.on log out delete/update records from db.

or put a flag in ur users table and update flag as y/n every time a user logged in / log out.

or something similar to this.

If user closes browser without logout then when next time user tries to log in. u can check previous active sessions for that user, if there? give a window to user that last logout was not correct and take any event from that user to update time or u can ask user to enter log out time (estimated) for late session or if users are not interested to select time u can update logout with a logout time . make a slandered interval of login duration.

think like this way......

OTHER TIPS

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.

Instead of create base controller and extend all other controllers from that, we can create and use Zend Front Controller plugin ( see examples here) to register all request to our website or web application.

class App_Plugin_RegisterActivity extends Zend_Controller_Plugin_Abstract
{
    public function dispatchLoopShutdown() {

        // read user identity
        $auth = Zend_Auth::getInstance();
        $authStorage = $auth->getStorage();
        $identity = $authStorage->read();

        $userID = $identity->id;

        // update user's table with current timestamp
        .....

    }
}

Then we count active users and much more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top