Honesty Rocks! truth rules.

Who's Logged In? I know how to start.. but not how to end..

HOME      >>       Programming



I wanted to know a "who's logged in 'counter'"..
It's very easy to make it write the name.. Just change some value (let's say "logged_in" to 1) in the database, when the character login. But how do I do when I want it to change back to 0, if the user logout? many users just close their browsers.. It's easy to add it to a logout script.. just make an update query.. but how do I do, so that the value change back to 0, when the user close his/her browser?

And.. another thing.. I am having this small problem..

Warning: Division by zero in X\XX\XX\XXX\XX.X on line 27

This is line 27, some rows above, and some rows under it..

$password=$userid['---'];$email=$userid['---'];$admin=$userid['----'];$health=$userid['----'];$spirit=$userid['----'];$stamina=(int)($userid['----'] / $userid['----']) * 100 ; # LINE 27?>

I include this file, to my index.php file.. and I get the Division by zero error.. I think I am using the "int" wrong..
any Idea how I can fix this?


Hm, this is just ideas as I forget how php handles this stuff from server side, but I would imagine you could do something along the lines of sessions or cookies, say for example you have cookies for all logged in users that expire in 5 minutes but are refreshed every time they switch to a new page. This means that you'll have a cookie per user with basic information in it if they are active, and once they are inactive for 5 minutes they are taken from the list.Similarly a script could be used to add active users to a table a log there last page access, similarly scanning this list every so often for users that have 'timed out', meaning surpassed an inactive time limit chosen by you.I'm sure someone will come along with a more precise idea but these just popped into my head so I thought I'd share in case they help. They might not even be viable given the technologies in use.


As Jeigh suggested, you need to store the last time someone accessed a page on your site in the database. You don't even need to clear it - it can be used somewhere else, now that you have it. And when you want to display all the users that were active in the last 5 or so minutes, you would SELECT all users from the database whose access time is less then 5 minutes before the current time.


As what Jeigh and Feelay had suggested. I would like to add that you can also have ajax on your main page that calls a php page on your server side as to update the "last active" time. This is in case you have an active page that display some stats which also use ajax to refresh it's content. That the user might not change page at all.I'm using this "last active" to control multiple-login as well. If the user does not log out manually, it has to wait for the timeout before he can login again on another pc. And I have a ajax script that logout the old page once it reaches timeout, for security reason.


Yes, sometimes when you create a simple login page, even a timestamp and ~10 minutes the user is logged in, it's a bit better to make him or her to stay logged on if the page is not closed, even though it's not necessary.Another problem sometimes occurs, I for example have a login logout page and I just use a simple session and not my own cookies and when I login and start writing an article, it takes usually more than 10 minutes or something like that and I hate to submit and see the please login page, I never fixed that problem though, but I feel more secure then if I leave my page open that no one else will do something, so you need to think how you want it to work. ;)