When user login in my website the table update in database that the user is online and when they click on logout the table updated in database that the user is offline, it working fine but the problem is that when user login and close the browser without logout their account and my table show that the user is online but I want that when user close the browser without logout their account the table updated that this user now offline tells me how can I do this a custom setup
Asked
Active
Viewed 260 times
-1
-
It sounds like you need session/user management library to keep the database up to date. We can recommend a few but which PHP framework are you using or is this a custom setup? – Useless Intern Sep 19 '18 at 17:42
-
1Possible duplicate of [Updating MySQL database when user closes browser](https://stackoverflow.com/questions/12403458/updating-mysql-database-when-user-closes-browser) – Tom Udding Sep 19 '18 at 17:47
-
welcome to stackoverflow. you can find many duplicates if you just do a simple search. but the short answer is : you can add a last activity time to your database and have ttl – Shahrokhian Sep 19 '18 at 18:04
-
You can use [`navigator.sendBeacon()`](https://stackoverflow.com/a/52412486/811240) to send an asynchronous request to the server to notify that the user has left. – Mike Sep 19 '18 at 18:57
1 Answers
0
Hit the server at a regular interval ( say 30 Seconds ) using Ajax and store the last activity date/time in the database. If the last activity is older than 30 seconds that means the user has left the site.

pradeep pandey
- 54
- 4