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
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...