By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,616 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,616 IT Pros & Developers. It's quick & easy.

Am I online?

P: n/a
I am setting up a web community with a feature that will display
whether or not a member is currently online.
I intend on implementing this by holding a column in the dbase simply
called online. When a user logs on, the status of this will be updated
to yes.

When the user clicks to log out, we simply change the state of this
field to no.
The problem comes when a user does not execute the log out script and
simply closes their browser, effectively logging them out.

How can I keep the state of the online attribute accurate? I have had
a few ideas such as using a JavaScript sniffer which detects when the
windows is closed then updates the dbase, however I don't believe such
a jscript exists?

Another possible solution would be to have a cron job but Im not sure
what the cron would check for and I feel there must be a better way
anyway!

Any suggestions welcomed!
Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
David wrote:
I am setting up a web community with a feature that will display
whether or not a member is currently online.
I intend on implementing this by holding a column in the dbase simply
called online. When a user logs on, the status of this will be updated
to yes.

When the user clicks to log out, we simply change the state of this
field to no.
The problem comes when a user does not execute the log out script and
simply closes their browser, effectively logging them out.

How can I keep the state of the online attribute accurate? I have had
a few ideas such as using a JavaScript sniffer which detects when the
windows is closed then updates the dbase, however I don't believe such
a jscript exists?

Another possible solution would be to have a cron job but Im not sure
what the cron would check for and I feel there must be a better way
anyway!

Any suggestions welcomed!


Hi David,

The simplest solution is to store a timestamp and a sessionid in that table.
After every request by this user, update the timestap. (updatescript)

Now decide on a reasonable timeout X (30 minutes eg).

Now delete all records that are older than (now - X).
You could use a cronjob, but a simple delete from tblWhoIsOnline where (...)
could do the job too. Just run that Query every time you use the
updatescript.

You probabaly want to put all that code in an include you use on every
php-page.

Good luck,
Erwin Moller
Jul 17 '05 #2

P: n/a
David wrote:
I am setting up a web community with a feature that will display
whether or not a member is currently online.
I intend on implementing this by holding a column in the dbase simply
called online. When a user logs on, the status of this will be updated
to yes.

When the user clicks to log out, we simply change the state of this
field to no.
The problem comes when a user does not execute the log out script and
simply closes their browser, effectively logging them out.

How can I keep the state of the online attribute accurate? I have had
a few ideas such as using a JavaScript sniffer which detects when the
windows is closed then updates the dbase, however I don't believe such
a jscript exists?

Another possible solution would be to have a cron job but Im not sure
what the cron would check for and I feel there must be a better way
anyway!

Any suggestions welcomed!


If you wanted to be adventurous you could write your own session
handling functions and store those in the database, not sure how
difficult it is, I have never done it but info is at...
http://uk.php.net/manual/en/function...ve-handler.php

~Cameron
Jul 17 '05 #3

P: n/a
bi****@hotmail.com (David) wrote in message news:<3e**************************@posting.google. com>...
I am setting up a web community with a feature that will display
whether or not a member is currently online.
I intend on implementing this by holding a column in the dbase simply
called online. When a user logs on, the status of this will be updated
to yes.

When the user clicks to log out, we simply change the state of this
field to no.
The problem comes when a user does not execute the log out script and
simply closes their browser, effectively logging them out.

How can I keep the state of the online attribute accurate? I have had
a few ideas such as using a JavaScript sniffer which detects when the
windows is closed then updates the dbase, however I don't believe such
a jscript exists?

Another possible solution would be to have a cron job but Im not sure
what the cron would check for and I feel there must be a better way
anyway!

Any suggestions welcomed!


Look at the previous discussions.

- http://groups.google.com/groups?thre...ing.google.com

- http://groups.google.com/groups?thre...ing.google.com

HTH

--
"I don't believe in the God who doesn't give me food, but shows me
heaven!" -- Swami Vivekanandha
Email: rrjanbiah-at-Y!com
Jul 17 '05 #4

P: n/a
What about keeping track of when the user last did something--came
online, viewed a page, etc. Every time someone needs to check the
"online" table you could update the status of everyone who hasn't had
any activity in the last 5 minutes to be "offline."

So it's three steps. First, update a user's last_action_time value
with every new page view. Then with each request to see who's online,
you update everyone's status who hasn't been active lately. Last,
retrieve information about who is actually "active".

Might that work?
MKatz843

bi****@hotmail.com (David) wrote in message news:<3e**************************@posting.google. com>...
I am setting up a web community with a feature that will display
whether or not a member is currently online.
I intend on implementing this by holding a column in the dbase simply
called online. When a user logs on, the status of this will be updated
to yes.

When the user clicks to log out, we simply change the state of this
field to no.
The problem comes when a user does not execute the log out script and
simply closes their browser, effectively logging them out.

How can I keep the state of the online attribute accurate? I have had
a few ideas such as using a JavaScript sniffer which detects when the
windows is closed then updates the dbase, however I don't believe such
a jscript exists?

Another possible solution would be to have a cron job but Im not sure
what the cron would check for and I feel there must be a better way
anyway!

Any suggestions welcomed!

Jul 17 '05 #5

P: n/a
I tend to agree with (M. Katz).

After messing around with sessions all night, and using a database to
handle sessions myself I have come to the conclusion that it is
unessasary.

By updating a column "time" in the dbase where the user=currentUser to
time()+ X every time a user loads a new page.

Then having a second script which is executed on every page (or maybe
a cron job) which goes through searching for users whom have their
"time" value > current time then setting their "is_online" attribute
to equal no.

Now I cant see the need for sessionIDs or, limiting the duration of a
session or anything other that what I mentioned!

There must be something wrong with my idea, surely?
Jul 17 '05 #6

P: n/a
I would approach the problem directly. Basically what you want to know is
the users who have an active session. So scan the folder where the PHP
session files are kept--you can obtain the path with a call to
session_save_path(). Open each of them and pass the content to
session_decode() to get the variables in the sessions.

$current_sess_str = session_encode(); // save current session
$active_sessions = array($_SESSION); // add the current session to the
list
$sess_path = session_save_path();
if($dir = opendir($sess_path)) {
while($file = readdir($dir)) {
if(is_file("$sess_path/$file")) {
if($sess_str = @file_get_contents("$sess_path/$file")) {
$_SESSION = array();
session_decode($sess_str);
$active_sessions[] = $_SESSION;
}
}
}
}
$_SESSION = array();
session_decode($current_sess_str); // restore current session

When the session expires or when you call session_destroy(), PHP removes the
session file.

Uzytkownik "David" <bi****@hotmail.com> napisal w wiadomosci
news:3e**************************@posting.google.c om...
I am setting up a web community with a feature that will display
whether or not a member is currently online.
I intend on implementing this by holding a column in the dbase simply
called online. When a user logs on, the status of this will be updated
to yes.

When the user clicks to log out, we simply change the state of this
field to no.
The problem comes when a user does not execute the log out script and
simply closes their browser, effectively logging them out.

How can I keep the state of the online attribute accurate? I have had
a few ideas such as using a JavaScript sniffer which detects when the
windows is closed then updates the dbase, however I don't believe such
a jscript exists?

Another possible solution would be to have a cron job but Im not sure
what the cron would check for and I feel there must be a better way
anyway!

Any suggestions welcomed!

Jul 17 '05 #7

P: n/a
"David" <bi****@hotmail.com> wrote in message
news:3e**************************@posting.google.c om...
I am setting up a web community with a feature that will display
whether or not a member is currently online.
I intend on implementing this by holding a column in the dbase simply
called online. When a user logs on, the status of this will be updated
to yes.
.........................
Any suggestions welcomed!


I use a simple db not another table in a db, but one if its own, and I just
update a timestamp [time()] for each user when they load a page.

to see who is online, I run through and list off all those < time limit
I have one of those, idle for 5 minutes, 10min, 15 min, offline type of
things

--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #8

P: n/a
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<is********************@comcast.com>...
I would approach the problem directly. Basically what you want to know is
the users who have an active session. So scan the folder where the PHP
session files are kept--you can obtain the path with a call to
session_save_path(). Open each of them and pass the content to
session_decode() to get the variables in the sessions.

$current_sess_str = session_encode(); // save current session
$active_sessions = array($_SESSION); // add the current session to the
list
$sess_path = session_save_path();
if($dir = opendir($sess_path)) {
while($file = readdir($dir)) {
if(is_file("$sess_path/$file")) {
if($sess_str = @file_get_contents("$sess_path/$file")) {
$_SESSION = array();
session_decode($sess_str);
$active_sessions[] = $_SESSION;
}
}
}
}
$_SESSION = array();
session_decode($current_sess_str); // restore current session

When the session expires or when you call session_destroy(), PHP removes the
session file.


This method is still not perfect. See previous discussions
<http://groups.google.com/groups?threadm=abc4d8b8.0311040007.75d45aeb%40post ing.google.com>

AFAIK, in PHP, the best method is to go for custom session handler,
so that you may have more control over the sessions (esp you may
simulate gc, and count guests & users easily).

--
"Success = 10% sweat + 90% tears"
Email: rrjanbiah-at-Y!com
Jul 17 '05 #9

P: n/a
Regarding this well-known quote, often attributed to David's famous "2 Feb
2004 15:15:57 -0800" speech:
I tend to agree with (M. Katz).

After messing around with sessions all night, and using a database to
handle sessions myself I have come to the conclusion that it is
unessasary.

By updating a column "time" in the dbase where the user=currentUser to
time()+ X every time a user loads a new page.

Then having a second script which is executed on every page (or maybe
a cron job) which goes through searching for users whom have their
"time" value > current time then setting their "is_online" attribute
to equal no.

Now I cant see the need for sessionIDs or, limiting the duration of a
session or anything other that what I mentioned!

There must be something wrong with my idea, surely?


Well, to make it easier on the server processor load, you only really need
to run the "housecleaning" script when someone tries to view an
online/offline status. It's a "tree falls in the woods" situation. If no
one's looking, does that person's status matter?

For that matter, you might not even need to traverse the whole table if
it's just a "My Friends" list. You'd only need to check for expiration on
usernames people were actually trying to look at.

For that matter, you might not even need to have that "expired" entry. Just
have a "LastTimeOnline" column, and say "if LastTimeOnline < now-5, they're
offline". You'd have the added benefit of a "Last Seen" entry, if you ever
wanted it, as well as a smaller table.

--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.