Apache, PHP and "dead" usersessions .. Anyone knows how?
Hi,
Is there a way to check if a session is "dead"? :scratch: Here's what i want: When a user logs in I set some session variables, and write into the Database that the user-status is "online" Next when the user exits the site, or closes the browser, I want to write into the database that the user is "offline". Its a bit like its done here: You can see which users are in a forum. However, when I close my browser, or leave the site, the information about me being gone, must be updated. How can you check this? I am using Apache/1.3.27 (Unix) with PHP 4.1.2 Thanks!! |
You can't check it. You need to make a user 'offline' after a timeout (10min or any other value you may find correct in your situation).
|
How can i create such a timeout? (Is that a cronjob or something like that?)
Im quit new in this matter... |
AFIR you can do a trick. Write your HTML code using OnClose event and a JavaScript function. So when the user close the browser or change the site your function can report it to you.
|
Thanks dorian, I think that the way i want to go, but then, can you help me further?
I use a mysql database, in which i keep the userstate (Just an INT for on / offline) . What can I do with the JavaScript? Or what other tricks can I do? |
You will need timeout, too. Browsers sometimes crash, users disconnect before they close browser etc. I don't know PHP very well, but I guess you need to run a loop from time to time and see if last activity time plus timeout is smaller than current time. If so, you mark the user "dead".
|
Thanx for the input so far...
But its indeed this time-out thingie: Who runs this? I guess some kind of script on the server, and not particularly a webserver/webpage/php script ?????? I indeed want to do something like this: currentTime>lastActiveTime+timeOut?userLoggedOff:doNothing But lastActiveTime is set by who? And this timeOut, where to put that? And then the script to run: Where to put that? As you see alot of questions......... Sorry for my l8 replies, but I am not always able to respond right away, but I always will respond!!! Thanks in advance for anyone who helps me further! |
It can be another process (one that wakes up only to check timeouts and then sleeps). The timeout may be set in a config file/databese entry.
|
Mara,
I still am lost here.... Is it some cron-process that should check this, or is there some other trick? I really have no clue where to start... Can you help me to start somewhere??? |
It may be a separate program started by cron. Or a prog that runs 'sleep(delay)' (or similar instruction, depending on the language you use to write it) after work is done, after a delay it wakes up and starts its work, then sleeps ans do on. I think a cron prog will be better.
|
If I was doing it, I'd have the 'online' and 'last_active' fields in my database. As the logged in user moves from page to page, the last_active is updated to the timestamp of when the page was served. Then, when you're creating your "Who's Online" box or whatever, you check the database for all users who are online, and have a 'last_active' time within the last e.g. 10 mins, display all of them. Those who are 'Online', but outside of the 10 mins are set to False in the 'online' field .
A cron job may or may not be more efficient. Haven't tried it before so I'm not sure. |
Thanks Obi,
I now have a good idea of where to start! Just if you were curious how, and maybe for all others: I will add the online and last access time to the database. Next I will create a php-script something like showOnline.php In this script I will check all entries in the database who have the status Online = true. All these entries will be checked on their last access time. If it exceeds the MAX_TIMEOUT (i.e. 10 minutes), this script will update the database, and set Onlinestatus to false. Now i include this script in every page on the site. So, the updating is done by the next user who gets to the site. (Or every time a user gets to another page on the site. ) I am aware of the amount of database activity this will cost, but what the heck, it's a very small site anyway. If it was intended for more than 1000 users a day, I would use something else than just PHP and mySQL. (Something like websphere... ) Thanks for your help and ideas!!! Satch |
All times are GMT -5. The time now is 07:03 PM. |