PHP Count Logged-In Users

后端 未结 3 812
醉酒成梦
醉酒成梦 2020-12-09 00:24

I\'m having trouble figuring out how I can count the number of logged-in users in my application.

What I have: When a user logs in, they get a sessi

3条回答
  •  轮回少年
    2020-12-09 01:02

    Rather than a IsLoggedIn column, you should add a LastTimeSeen column. Any time a person visits a page you update the column:

    UPDATE members SET LastTimeSeen = NOW() WHERE id = $the_user_id
    

    Then to get how many people are on the site at any given moment you use the query:

    SELECT COUNT(*) FROM members WHERE LastTimeSeen > DATE_SUB(NOW(), INTERVAL 5 MINUTE)
    

    That shows how many people have viewed a page in the past 5 minutes, which is the best you're gonna get without a much more complicated solution.

提交回复
热议问题