PHP - Who's Online Script Help

masterjake

New Member
Messages
73
Reaction score
0
Points
0
Hi! I was messing around and thought of a way to make a who's online script the easy way. Basically, in my members database I have a field called "online" which is default set to 0. When they click login, if it's a success then a MySQL statement runs which sets online to 1. I also have a small script to test if anyone has online set to 1 and if so display their username to the right of the page. The only problem with this is that a lot of people never actually click the logout button and I have it set online back to 0 when they click logout. This shows them as online forever when they actually aren't.

So, I decided to get help with an actual who's online script. Can someone please post one for me =]?
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Most often this is done with a field which stores the timestamp of the user's last action. In every page, a query should execute(usually from an include()'d script) which updates the last action field for the user with the time of their current action. Like this (assuming $userid holds the current user's id):

UPDATE users SET lastaction = UNIX_TIMESTAMP() WHERE userid = '$userid'

Then when you want to determine who's "online", you just select the users who have been active recently. For example, to get users who had been active within the last 5 minutes you could use:

SELECT username FROM users WHERE lastaction > (UNIX_TIMESTAMP() - 300)

MySQL timestamps are in seconds, so 300 is used for 5 minutes since it's 5 * 60.
 

masterjake

New Member
Messages
73
Reaction score
0
Points
0
Thank you very much, I'm going to go ahead and try to set one up =]

ADDED TO YOUR REP =]
 
Last edited:

phpasks

New Member
Messages
145
Reaction score
0
Points
0
PHP:
<?php
select username from user_table 
                     where (UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(current_timeslice))>='5400'
?>

You can also check time, no visit then logout those member
 
Top