Frank Vèssia Posted January 12, 2014 Share Posted January 12, 2014 There's a built in way to get only loggedin users inside $users->find selector? Link to comment Share on other sites More sharing options...
RyanJ Posted January 12, 2014 Share Posted January 12, 2014 $user->isLoggedin() maybe? Link to comment Share on other sites More sharing options...
teppo Posted January 12, 2014 Share Posted January 12, 2014 @Sevarf2: no native way (that I'm aware of). In case tat you're using Session Handler Database (i.e. storing sessions to database instead of files) table "sessions" will contain all active sessions and theoretically you could get distinct user_id's from there and then find users matching those. These users may not actually be actively doing anything or even really be online, they just haven't logged out yet and their sessions haven't been cleaned by garbage collector. Sounds kind of hacky, but it's a start.. 3 Link to comment Share on other sites More sharing options...
adrian Posted January 12, 2014 Share Posted January 12, 2014 Related thread: http://processwire.com/talk/topic/4553-how-do-i-get-a-list-of-online-users/ 2 Link to comment Share on other sites More sharing options...
Frank Vèssia Posted January 12, 2014 Author Share Posted January 12, 2014 @teppo i think it's the only way at the moment, thanks I'll try...my actual way is to add a custom field in user template and run this code on each page of my site: if ($user->isLoggedin()){ $lastActivity = time(); $user->of(false); $user->lastactivitypw = $lastActivity; $user->save(); $user->of(true); } Than I can use normal selector to get the list of online users based on 1 minute delay.My problem is I think this method is not correct or not optimized because I write the database on each page request... Link to comment Share on other sites More sharing options...
Frank Vèssia Posted January 12, 2014 Author Share Posted January 12, 2014 I ended up with this code, extract from the session db handler module, modified excluding guest users and focusing just on count: <? function onlineUsers(){ $mins = 1; $table = SessionHandlerDB::dbTableName; $sql = "SELECT COUNT(*) FROM $table " . "WHERE ts > '" . date('Y-m-d H:i:s', (time() - ($mins * 60))) . "' " . "AND user_id!=40 " . "ORDER BY ts DESC LIMIT 1000"; $result = wire('db')->query($sql); $row = $result->fetch_array(); return $row[0]; } 3 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now