The easiest way to do this would be to set a column "lastactivity" to the current time every time the user loads any of your pages (just build it into your authentication script). You can then classify someone as "online" if they have been active in the past 5 mins.
Hope that's close enough to what you're after.
humbug
Junior Poster in Training
93 posts since Oct 2005
Reputation Points: 20
Solved Threads: 13
pritaeas
Posting Expert
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
<?php
if (! session_id()) {
session_start();
}
if (! isset($_SESSION['timestamp'])) {
$_SESSION['timestamp'] = time();
}
if (time() - $_SESSION['timestamp'] < 60) {
$_SESSION['timestamp'] = time();
echo "More time added "; //to see if it works
}
else {
mysql_query("UPDATE user SET onoffstatus = 'Offline' WHERE username = '$userfinal'");
session_unset(); # Session_unset and Session_destroy
session_destroy(); # Will remove all sessions.
header("location: index.php"); # This code will send you back to the index page
}
?>
pritaeas
Posting Expert
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
Use a cron job to set users offline after a certain amount of time.
Just have it run every 5-10 minutes and that will solve the problem without having to rely on javascript to do it (which is not a good thing to do).
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
Its best to check with help and support.
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
You will have to modify the code and maybe your database.
Pretty much you will need to use humbugs' solution.
In the cron file you will need to select all users that have not been active for lets say 10 minutes. Then run an update query for each user returned to set their status to 'offline'.
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
Sorry for not getting back to you about my idea. What I meant was, you store the timestamp for each user's last activity in the database. Then any time you want to check if someone is "active" you grab the timestamp and make the comparison.
You can also get a list of "active" user by doing (psuedocode) "SELECT * FROM user_activity WHERE timestamp>TIME()-60*5"
All you need to do for this is update the user's "last activity" timestamp every time they request a page, and you'll be laughing.
If you want it to be more accurate you can use AJAX to send a request to the server every 4 mins to tell the server that the user still has the webpage open in his/her browser.
Hope that helped. (I'm not saying the other way won't work either..)
humbug
Junior Poster in Training
93 posts since Oct 2005
Reputation Points: 20
Solved Threads: 13
For each user you want to check, you would do something like this:
1. Get the timestamp for this user from the database
SELECT last_activity FROM user_activity WHERE id=$id
2. Compare this value with the timestamp for 5 mins ago within PHP if ($last_activity > $now-60*5) { echo "online" }
I couldn't be bothered looking up the PHP code for getting timestamps, or translating to and from MySQL timestamps, but you can do that :P
humbug
Junior Poster in Training
93 posts since Oct 2005
Reputation Points: 20
Solved Threads: 13