How to check logged in online users using Zend Framework
Asked Answered
T

3

7

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.

Templet answered 9/12, 2010 at 0:29 Comment(0)
V
2

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......

Vespid answered 9/12, 2010 at 2:9 Comment(3)
The problem with this method is not all users will logout each time they leave the site, they might just close the browser and leave. In that case there is no way I can automatically delete those sessions from my db. So this method basically is flawed.Templet
No its not Flawed , you can add another column lastTimestamp and update it every time you check if he is logged in aka open a new page , so if the user closes his/her browser he would have old timestamp so you can logout those people automatically via cron that runs every 30 min as an exampleUredo
This lasttimestamp idea looks cool, I shall try it tonight and let you know guys!Templet
T
4

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.

Tenderloin answered 13/12, 2011 at 19:1 Comment(1)
It would be nicer to add a controller_action_helper, a plugin or use the postDispatch() method so you won't have to parent::init() every time. Also, you can use postDispatch() so that your stuff won't get executed when you are doing a json/xml request.Tish
V
2

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......

Vespid answered 9/12, 2010 at 2:9 Comment(3)
The problem with this method is not all users will logout each time they leave the site, they might just close the browser and leave. In that case there is no way I can automatically delete those sessions from my db. So this method basically is flawed.Templet
No its not Flawed , you can add another column lastTimestamp and update it every time you check if he is logged in aka open a new page , so if the user closes his/her browser he would have old timestamp so you can logout those people automatically via cron that runs every 30 min as an exampleUredo
This lasttimestamp idea looks cool, I shall try it tonight and let you know guys!Templet
B
1

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.

Becka answered 29/5, 2013 at 8:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.