Having this type of thing depending on a user interaction is not a good idea, if the computer suddenly hangs up (power failure, shutdown because it crashed, etc.), the data in the database will not be updated
I imagine that in the database you have a online
column that is set to true or false, I suggest that you change to a date type ( datetime
or timestamp
), when the login is set in this field , the current date plus a 5-minute interval, for example. In the frontend, create a setInterval
that makes an ajax request every 5min to refresh the field again. If a setInterval
is not possible, this update can be done in the database whenever the user makes a request to the backend, thus the database data is outdated for a maximum of 5min
But beware, depending on how many users have been using the application at the same time, can make the application too cumbersome
Since you did not specify which database you are using, I thought it was a SQL like MySQL