waqas87 Posted August 24, 2010 Share Posted August 24, 2010 i am trying to set user status as offline when user close browser but i am facing a problem ... i am calling a function when body unload which will set guest user status to "0" when user close the browser but it not working ... it execute php before function is called.. any idea why this is happening ...plz help it's making me crazy Link to comment https://forums.phpfreaks.com/topic/211651-javascriptphp-problem-on-browser-closeunload-event/ Share on other sites More sharing options...
kenrbnsn Posted August 24, 2010 Share Posted August 24, 2010 Code? Link to comment https://forums.phpfreaks.com/topic/211651-javascriptphp-problem-on-browser-closeunload-event/#findComment-1103346 Share on other sites More sharing options...
nethnet Posted August 24, 2010 Share Posted August 24, 2010 You may be better of just recording a users timestamp when they access a page while logged in, and track that in your database. After, say, 5 minutes of inactivity, you could consider them offline. This is how most online/offline schematics work. As far as I know there is no way to force a log off when a user closes a browser by simple javascript. Feel free to prove me wrong on that, though One method that does come to mind though would be establishing a simple AJAX 'ping' of sorts. While a user is logged in, you could be constantly pinging the server with a simple PHP file executed though AJAX. If the ping occurs every 10 seconds, you could consider any 'most recent ping' of a user that is more than 10 seconds ago to be indicative that they have either closed the browser, navigated away from the site, or logged out. I'm curious to see the resolution of this thread, though... Good luck, Theo Link to comment https://forums.phpfreaks.com/topic/211651-javascriptphp-problem-on-browser-closeunload-event/#findComment-1103348 Share on other sites More sharing options...
waqas87 Posted August 25, 2010 Author Share Posted August 25, 2010 sorry about here it is { <?php $Gueststatus="update tbl_guestuser set STATUS='0' Where USERNAME='$userG'"; mysql_query($Gueststatus); $Userstatus="update tbl_signup set loginststus='0' Where MemberUserName='$user'"; mysql_query($Userstatus); ?> alert('hi'); } Link to comment https://forums.phpfreaks.com/topic/211651-javascriptphp-problem-on-browser-closeunload-event/#findComment-1103445 Share on other sites More sharing options...
Alex Posted August 25, 2010 Share Posted August 25, 2010 You can't use PHP like that. PHP is a server-side language. It runs the code, sends the output to the browser and has nothing to do with anything after that. nethnet's solution is much better (it actually works ). Link to comment https://forums.phpfreaks.com/topic/211651-javascriptphp-problem-on-browser-closeunload-event/#findComment-1103447 Share on other sites More sharing options...
waqas87 Posted August 25, 2010 Author Share Posted August 25, 2010 any help on Ajax ping code i Google it but found nothing .... Link to comment https://forums.phpfreaks.com/topic/211651-javascriptphp-problem-on-browser-closeunload-event/#findComment-1103451 Share on other sites More sharing options...
Alex Posted August 25, 2010 Share Posted August 25, 2010 You don't need to use that AJAX method unless it's necessary that stats are updated that fast, which in most cases it's not. You can just set the timeout to something like 5 minutes. Every time the user requests a page store the time of that request. If the last time they visited the page is over 5 minutes (or whatever you set your timeout value to be) then you can assume they're offline. Link to comment https://forums.phpfreaks.com/topic/211651-javascriptphp-problem-on-browser-closeunload-event/#findComment-1103453 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.