Display logged in members

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Display logged in members

Post by Jim_Bo »

Hi,

Whats the best way to echo (display the user names) an accurate reading of the user names that are logged in?

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

search: who's online.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hey,

This code below .. below suits my user database .. It seems to echo all users rather than only users that have been active for less that 5 minutes?

Code: Select all

$time = time();
$timeonline = '300'; 
$timedifference = time() - $timeonline;
$whosonline = mysql_query("SELECT * FROM users WHERE last_login >= '$timedifference'");
while($whosonlinearray = mysql_fetch_array($whosonline)){
echo $whosonlinearray['alias'];
}
Thanks
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Whats the type of last_login ? Date, Time, Integer, Varchar ?
If its any of the date/time types then this should work :
$whosonline = mysql_query("SELECT * FROM users WHERE UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(last_login) <=500");
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Im using now() to insert with date/time as setting .. Your code works great thanks ..

Another question with sessions .. When people logout things work fine .. but when they just close the browser the session stays active for like 5 minutes .. giving false readings of whos online etc etc .. Can anything be done so when the browser is closed .. the session is destroyed at the same time? How are things done witihn the likes of devnetworks forum etc .. or is the session timout the best answer ..?

Cheers
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the timeout is what kills the session if they don't log out. It works just fine.. There's almost no way to know definitively who is looking at a particular page from second to second, because that request is the only live connection you will typically have. Unless you are using a realtime media connection, there is little way to know all the time.

These things are a basic snapshot of who has been active in the last x minutes, nothing more.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hey,

Great .. Thanks for that explaination ..
Post Reply