title: How to keep track of users who have logged off so that they do not appear in the Online Now page?
body: <p>On one of my sites I must keep track of users that are online.  </p>
  
  <p>There is a "login activity" table that keeps track of every user's login, so there are multiple rows per user in that table, one for each time a user has logged in (and whether they actually logged themselves out in that session).  This table was previously used to keep track of online users but wasn't very accurate because the script was considering any user that logged in in the last 10 minutes as being online unless they specifically logged off, at which point the row that was inserted during that login session has its "logged in" column set to "no".  The problem with this solution is that active users who had logged in over 10 minutes ago were not appearing "online".</p>
  
  <p>To improve upon this, I've now added a "last activity" column to the main user table to keep track of the time of each user's last pageview and I'm calculating the "online now" as simply any user whose last activity was no more than 10 minutes ago.</p>
  
  <p>However, how do I keep users who had activity in the last 10 minutes <em>but who have logged off</em> from appearing in the "online now" page?  Do I need to create a new table with one row per user just to keep track of when a user logged in and whether they logged themselves off?  Can I somehow use the existing "login activity" table even though it has multiple rows per user and query its most recent row to see if the user logged themselves off (but then how would I check the user's most recent "login activity" row as one of the criteria in a large join query?)?  Is there a simpler way?</p>