3

I have a script that will login and logout a user. It works perfectly. Now I have like a widget that counts how many users are registered and activated as well as how many users are online. I do this by having a field in my users database that says online = 1 or 0. When the person logs in, online = 1 and logs out online = 0. Now I haven't taken into account that this field is only being updated because the user is doing something. I haven't taken into account that the session would timeout.

How can I make a function that says something like if session timeout = true then update users set online=0 where username=$username and user_id=$user_id.

Harley Frank
  • 149
  • 2
  • 11

4 Answers4

3

Instead of trying to use a boolean value to see if somebody is logged in, try using a TIMESTAMP. Then you can perform more accurate logic based on how long somebody has been away. If the last time somebody has loaded a page on your website was 30 minutes ago, do you think they're online? Do you even think they're at their keyboard?

Andrew
  • 546
  • 4
  • 17
3

In your database table, add another column something like last_seen. Update this every time you see your users online. After a certain period of inactivity, they will be marked as inactive. In fact, I suggest you replace your online field with this.

For example,

ALTER TABLE users CHANGE COLUMN `online` `online` DATETIME; -- SAMPLE SQL query only

To check how many users are online:

SELECT * FROM users WHERE online>(NOW()-INTERVAL 1 HOUR); -- SELECTS all users online in the past hour.

If the user logs out, you can simply set the online = NOW()-INTERVAL 1 HOUR. Or, you can also retain your previous online field and you can check if the user is idle (using my suggestion) OR online=0.

rationalboss
  • 5,330
  • 3
  • 30
  • 50
  • When logging in to the page update users set last_action=UNIX_TIMESTAMP() or DATETIME()? where user_id=$user_id and add that to every page of the website when the user goes to. And in my user online function which is select count(`user_id`) from users where online=1 and last_action>(UNIX_TIMESTAMP()-3600)? – Harley Frank Mar 05 '13 at 19:48
  • Oops. Please see my corrected answer. UNIX_TIMESTAMP() returns an integer and it's not suitable for the DATETIME field. – rationalboss Mar 05 '13 at 21:08
  • In the field when I check the users online or not, if the date is past 5 minutes after login 2013-03-07 08:05:57, will it not show that user? – Harley Frank Mar 07 '13 at 13:07
  • you have to always update the online status. ex: `UPDATE users SET online=NOW() WHERE userid=123;` – rationalboss Mar 07 '13 at 16:33
  • I got it to work. I used a function that will be at the top of every page called last_action. What this does will `mysql_query("UPDATE users SET last_action=NOW() WHERE user_id='$user_id'");` To count how many users are online via last_action in a function: `return mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM users WHERE last_action>NOW()-300"), 0);` – Harley Frank Mar 08 '13 at 17:27
0

The session will only timeout if you want it to do so. This question is really a duplicate of 'How do I expire a PHP session?'

Code is only executed when a php page is served, so you will need to track the last time a user was active my using a session variable to track the last time a page was served to that user. Then, whenever serving any php page to the user, check to see if the timeout period has elapsed and log the user out if it has, see link for examples.

Community
  • 1
  • 1
Motomotes
  • 4,111
  • 1
  • 25
  • 24
0

Add lastOnline field which stores the timestamp of last user activity. Have some ajax function on the page which updates the timestamp every "n" seconds.

To check if user is online - check both: online field and timestamp. If timestamp was updates more than "n" seconds ago - user is offline even if online field is equal 1.

Mike Gordo
  • 121
  • 1
  • 4