Jump to content

select online users


Frank Vèssia
 Share

Recommended Posts

@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..  :)

  • Like 3
Link to comment
Share on other sites

@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

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];
}
  • Like 3
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...