Whats the easiest way to determine if a user is online? (PHP/MYSQL)

前端 未结 10 1246
轻奢々
轻奢々 2020-12-02 05:24

Is there a way I can piggy back sessions to know if the user is online?

I.e: use logs on, I set a $_SESSION variable, user times out- cookie Garbage collector update

10条回答
  •  时光说笑
    2020-12-02 06:03

    My way may not be the best way but since my site and userbase is all in mysql DB, when a user logins into my site,

    1. I update the user table to say they are online
    2. Insert them into an Online table
    3. Then I set a session with the current time

    Then on every page load I check for the online time session, if it exist, I check to see how old it is, if it is less then 5 minutes old, I do nothing, if it is older then 5 minutes, then I update the session time again with current time and also update the online users table with the time

    Then I have a cron job that runs every 10 - 15 minutes that deletes any uses from online table and marks there user table as offline if there online time has updated within X amount of minutes

提交回复
热议问题