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

前端 未结 10 1235
轻奢々
轻奢々 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:07

    You can also use the onunload tag... On the body More information here (https://www.w3schools.com/jsref/event_onunload.asp)

    This way you don't have to wait 4 -5 minutes for the user to get offline.

    But you should still use the ajax update method in-case the user's browser crashes...

    Edit: As @Quentin pointed out in the comments you may not be able to send out requests but there is a way you can do it... Explained in more detail in this Article.
    So basically they are using the sendBeacon() Here is an Example

    window.addEventListener("unload", function logData() {
      navigator.sendBeacon("/log", analyticsData);
    });
    

    NOTE: Although it should be kept in mind that this should not be the only method you should depend upon, and should implement for redundancy...

提交回复
热议问题