i need to perform a MySQL query on database when a user closes his browser.i am developing a application where when a user logs in a database field lo-gin_status is set to 1 and when a user logs out his lo-gin_status is set to 0.but what should i do if a user logs in but does not log-outs he just closes his browser.
-
3What have you tried? You don't seem to have made any effort to do this yourself... show what you have tried so far – naththedeveloper Jan 03 '14 at 12:38
-
You could combine something like [this answer](http://stackoverflow.com/questions/333665/javascript-to-get-an-alert-when-closing-the-browser-window) with an AJAX request which sets the flag to `0`, or have a timeout of the flag after x minutes of "inactivity" – naththedeveloper Jan 03 '14 at 12:39
-
http://stackoverflow.com/questions/16707249/detect-close-windows-event-by-jquery http://stackoverflow.com/questions/1631959/browser-window-close-event – Suresh Kamrushi Jan 03 '14 at 12:41
-
1Your trying to solve an already solved problem: simply set a timeout threshold. As long as the user interacts with your site, delay the threshold. If the user tries to interact after the threshold has run out, redirect him to the login page. – nietonfir Jan 03 '14 at 12:44
-
i have problem here, window unload function works when the page is refreshed.so it sets the login status 0.i used this to prevent multiple user access – Arya Jan 08 '14 at 10:04
4 Answers
You can try and do this with Javascript and check for a window close event, but it's up to the browser on whether that is supported.
What you could do instead is create a table to log when people visit your site, and then run a cron job to check how long it has been since they changed pages. If it's been longer than X minutes set that user's status to offline.
Try this anyways:
Use the window.onunload event.
https://developer.mozilla.org/en-US/docs/Web/API/window.onunload
You should be able to make an ajax call to run the query that you want to run from there

- 1,621
- 4
- 20
- 55
You can send some ajax request to server side on unload js event. Look here for details

- 620
- 4
- 12
- 31
To be precise, you are looking for the .unload()
jQuery event.
$( window ).unload(function() {
//call a php function that sets the flag to 0 or whatever you want
});

- 14,762
- 12
- 59
- 100
-
Answers the question, but it's not guaranteed that the request actually finishes. – nietonfir Jan 03 '14 at 12:42
-
-
-
This works in chrome on close event BUT in FF it works on reload also, which is not good. I don't want to perform query on reload. i want to perform on close – J4GD33P 51NGH Jul 28 '18 at 07:37
You are approaching this from the wrong view. There is no 100% sure way to tell if he logged out or closed the browser ( he might also just kill the browser or turn of pc/mobile device ).
Instead, store session data in DB and refresh it every time user does an action on the page and, if there was no action for a certain time ( like 30 minutes or so ), just log him off manually.

- 7,902
- 7
- 59
- 100