Jump to content

Displaying logged in users


johnsmith153

Recommended Posts

I want to display number of users actually logged in at the moment.

 

How would be the best way to do this?

 

Would you add a record to a dbase when they log in, then delete when they log out (then count records in that table to get the current loggd in number)?

 

If you did this what if they just closed the browser without logging out on the server?

 

 

Link to comment
https://forums.phpfreaks.com/topic/204356-displaying-logged-in-users/
Share on other sites

It sounds to me like you're on the right path. I'm a bit curious about this as well, however the only flaw I see with your idea is what if the user never logs out?

 

I think it might be more suitable if you was to run a cronjob every x minutes (15 is good) and check to see if the session still exists and if not remove from the user from active list in the database.


//validate user login

$activity = time() + 300; //5 mins
$query = "UPDATE users SET logged_in = $activity WHERE name = '" . mysql_real_escape_string($_POST['username']) . "' LIMIT 1";
$result = mysql_query($query);

//update user timestamp to time() + 300 on each page request.

 

usersonline

$now = time();

$query = "SELECT name FROM users WHERE logged_in >= $now ORDER BY id DESC";
$result = mysql_query($query);

   while ($row = mysql_fetch_row($result))
      echo $result[0] . ', ';

echo 'currently active: ' . mysql_num_rows($result);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.