Oli Filth wrote:
cyberhorse said the following on 12/06/2005 08:51: One reason to want to have something run on logout/session time out is
to keep track of online/offline users (many bulletin boards have to
deal with this problem).
One solution is to have a function call at the beginning of each page
that checks if it has been 5 minutes (or whatever time you want) since
the last action of all users. You keep track of all users yourself
through database entries or something similar and if some of them
appear inactive, you clean them up.
This can has a negative impact on performance as you are running a lot
of code that in most cases will do nothing
It shouldn't have that much of an effect if you push the clean-up into
the database query, i.e. you do something like:
UPDATE users SET online = 0 WHERE (lastOnlineTime + x) < NOW()
Although I haven't tested the speed of this, so I could be wrong! It's
almost certainly faster than doing it in manually in PHP though. (You
could probably optimise this by adding "online = 1 AND" into the WHERE
clause, and indexing the online column... maybe)
The speed problem can be cleared up if you use a column "sessionExpireTime",
and then index on sessionExpireTime, and then query like so:
UPDATE sessions SET online = 0
WHERE sessionExpireTime < NOW()
AND session <> session_id
....while this may seem like nitpicking, it ensures that the server will
always use the index, regardless of which server platform you are using.
It is also important to filter out the session you are working with, because
in a moment you are about to issue this query to refresh the current
session:
UPDATE sessions set sessionExpireTime = NOW() + x
WHERE sessionid = session_id
....and on a busy site the processes doing the first update will keep
deadlocking with the processes doing the second update and your site will
lock up.
--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)