php manage online offline users

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
bhagirath
Forum Newbie
Posts: 1
Joined: Sat Aug 14, 2010 12:39 pm

php manage online offline users

Post by bhagirath »

how to manage online users and show list of Online users to each online user and make each online user to offline even if he/she close browser or direct navigate by changing URL address without clicking on sign out or log out link. :banghead:
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php manage online offline users

Post by Jonah Bron »

There really is no surefire way of doing that. The best way is to use a timeout system. In the 'users' table, add a column that contains the timestamp of the last time they visited. If the timestamp is recent enough, list them as 'online'.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: php manage online offline users

Post by John Cartwright »

I haven't tested this in a very long time, but the following is used to count temporary session files in PHP's temp dir.

Code: Select all


define('TIMEOUT', 3 * 60); //seconds old until temporary file is no longer counted
$results = glob(session_save_path() . DIRECTORY_SEPARATOR .'sess_*');

$online = 0;
foreach ($results as $session) {
    if(time()- fileatime($session) < TIMEOUT) {
        $online++;
    } 
}

echo $online;
This of course only counts if the user has initiated a session at some point.
Post Reply