0

how to get currently logged users in my website. I have a mysql table for maintain users.

I don't know how to display names who are logged in.

aleroot
  • 71,077
  • 30
  • 176
  • 213
w3father
  • 569
  • 2
  • 11
  • 26
  • Too generic question, post at least the structure of the user table ... – aleroot Nov 01 '11 at 10:05
  • We expect you to have attempted to solve this problem by yourself rather than asking the community to arrive at a complete solution for you. When you've got some code to show us that demonstrates some effort by you (even if it's wrong) please update your question and flag to re-open. Thanks. – Kev Nov 01 '11 at 15:00

6 Answers6

6

Don't use a isOnline field as others suggested. Instead use a lastSeen field. You can put a timestamp in that field and update it either on every page view.

If you want to go easy on your system, you can update every couple of minutes (with a variable $lastUpdate in the user session containing the timestamp of the last update of lastSeen).

To find "online" users, you can select all the users that were lastSeen in the last X minutes, for instance 10 minutes.

You can't know an exact number of online users, because you can't know if an user is looking at your page or he/she has already closed it. Anyway, a solution like this one is a good approximation.

Every hour or every day, depending on how many visitors you have, you can run a process to delete old entries.

stivlo
  • 83,644
  • 31
  • 142
  • 199
2

There must be a field 'isOnline' in your users table, whose value will be either 0 or 1.

Whenever a user logs in, you update it to 1, and whenever user logs out, you'll update it to 0.

Currently online users can be retrieved by:

SELECT * FROM `users` WHERE `isOnline`='1'
Ananth
  • 4,227
  • 2
  • 20
  • 26
  • 1
    What if user doesn't log out and just leaves the page? Online forever :) – dwalldorf Nov 01 '11 at 10:07
  • @entek yes you are right. I have tried already with a flag column. What if user doesn't log out and just leaves the page? – w3father Nov 01 '11 at 10:11
  • So in addition you'd need some javascript or something like that, which checks if the user is still on your site or not. Maybe you'll find something [here](http://stackoverflow.com/questions/147636/best-way-to-detect-when-user-leaves-a-web-page) – dwalldorf Nov 01 '11 at 10:15
  • Oh, forget that, stivlo's solution seems very appropriate to me – dwalldorf Nov 01 '11 at 10:17
  • Yeah right, his solution is good. Mine's just a crude way :) – Ananth Nov 01 '11 at 10:18
1

My approach is to have table with user session tokens. Everytime user visits the site timestamp in session record is set to now. If session not touched for some amount of time it is expired and deleted from table (by cronjob or in some user requests as garbage collection). The same on logout. Structure is sessid, user_id, last_seen_ts. You can just join this table with users to fetch their names. Although, it can be done using only one table - store not only flag isOnline, but some timestamp to expire users that just left your site not logging out.

dmitry
  • 4,989
  • 5
  • 48
  • 72
0

You can create a column name isOnline and set it true when user get online. Then query it by isAvailable column

select * from users where isOnline = 'Y';
Fatih Donmez
  • 4,319
  • 3
  • 33
  • 45
0

just create a field in your users table into your database with any name like status that will suitable for you and update that filed with user login and logout with the value true on login and false on logout, and just check the true value that will provide you the all login users...

jogesh_pi
  • 9,762
  • 4
  • 37
  • 65
-1

I think you need to have a flag for that in your database or create a file for every user in some temporary directory.

dwalldorf
  • 1,379
  • 3
  • 12
  • 21