Apache, PHP and "dead" usersessions .. Anyone knows how?
Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418
Rep:
Apache, PHP and "dead" usersessions .. Anyone knows how?
Hi,
Is there a way to check if a session is "dead"?
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?
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.
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418
Original Poster
Rep:
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".
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418
Original Poster
Rep:
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 ??????
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.
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418
Original Poster
Rep:
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... )
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.