wezly Posted October 30, 2010 Share Posted October 30, 2010 Hi, What I would like to do is when a user closes the browser or tab, update a value in to mySQL cell via php. Either automatically or have a system message pop up that says "are you sure you want to close this window" and when they click yes it updates my database and then closes. I have minimal experience with java and after searching around I have not found anything useful bar the use of a possible 'on unLoad' event ? thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/ Share on other sites More sharing options...
.josh Posted October 30, 2010 Share Posted October 30, 2010 You should know that 213% of online users HATE those "are you sure you want to close this window" popups, NO good EVER comes from them, and if you somehow think your site is the exception and that you actually will have people who will say "omg you're right, I DON'T want to close this window, what was I thinking??" then you should pick another profession right now, because you have just demonstrated utter failure to understand the user base or your own product. If you want to decrease bounce rate on your site, then focus on providing quality, relevant content / products, not trying to trap people into staying on your site or making them jump through some extra hoop to leave. Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/#findComment-1128384 Share on other sites More sharing options...
gevans Posted October 30, 2010 Share Posted October 30, 2010 Just a thought, he may be making his own webmail client and wants an onunload event to warn the user that their closing their browser window without sending or saving a draft of an email their writting..... I know, I don' think so either, but you never know, there might be a good reason behind it! Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/#findComment-1128387 Share on other sites More sharing options...
phpfreak Posted October 30, 2010 Share Posted October 30, 2010 213% - love that CV! Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/#findComment-1128388 Share on other sites More sharing options...
Pikachu2000 Posted October 30, 2010 Share Posted October 30, 2010 Oh noooeessss!!! Teh windoh mite close! Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/#findComment-1128391 Share on other sites More sharing options...
.josh Posted October 30, 2010 Share Posted October 30, 2010 Just a thought, he may be making his own webmail client and wants an onunload event to warn the user that their closing their browser window without sending or saving a draft of an email their writting..... I know, I don' think so either, but you never know, there might be a good reason behind it! You are right, it is *possible* but... I somehow doubt someone versed enough to make their own webmail client would be asking a question like this. On a side note... dunno how active you are about reading posts in various forums around here, but this seems to be a recurring topic lately. I would almost tentatively call "homework!" but it does seem kind of an odd assignment to be giving....probably just a coincidence. Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/#findComment-1128395 Share on other sites More sharing options...
gevans Posted October 30, 2010 Share Posted October 30, 2010 Just a thought, he may be making his own webmail client and wants an onunload event to warn the user that their closing their browser window without sending or saving a draft of an email their writting..... I know, I don' think so either, but you never know, there might be a good reason behind it! You are right, it is *possible* but... I somehow doubt someone versed enough to make their own webmail client would be asking a question like this. On a side note... dunno how active you are about reading posts in various forums around here, but this seems to be a recurring topic lately. I would almost tentatively call "homework!" but it does seem kind of an odd assignment to be giving....probably just a coincidence. I completely agree, just thought I'd give an obvious example as a point. Haven't been around the forums for a while, it is a very random "homework" assignment, I hope people understand where this sort of functionality "should" be used. Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/#findComment-1128398 Share on other sites More sharing options...
wezly Posted October 30, 2010 Author Share Posted October 30, 2010 Hey people, thanks for you replies.. By no means do I want to 'Trap' people on the site, the reason that I need this function is so that my database knows when a user has left the page and to allow a new user to enter that page. as a Host. I would be perfectly happy not having the message, is there a way to use that function to tell php someone has closed the page? without the popup ? because I totally agree that I hate those messages! but as I have limited knowledge this seemed like a slightly easier option ?.. Wez. Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/#findComment-1128404 Share on other sites More sharing options...
Pikachu2000 Posted October 30, 2010 Share Posted October 30, 2010 Relying on an action from the client side for things such as this is generally a poor plan. Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/#findComment-1128411 Share on other sites More sharing options...
wezly Posted October 30, 2010 Author Share Posted October 30, 2010 The Client doesn't even need to know it is happening. I assume you guys know how to do this?.. I just need the java to tell php when they close the page so that I can update the table cell to 0 so the db knows there is no one connected. or if there is a better way of doing this please tell thanks. Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/#findComment-1128416 Share on other sites More sharing options...
Pikachu2000 Posted October 30, 2010 Share Posted October 30, 2010 When I refer to client side, I mean javascript. Javascript is a client side language. Using javascript for this is a bad idea since you'd then be relying on a client side action. Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/#findComment-1128422 Share on other sites More sharing options...
wezly Posted October 30, 2010 Author Share Posted October 30, 2010 Sorry, yes I must have miss read that. What I am relying on them doing is something they will inevitably do! Close The Browser ? and I trust javascript to tell me when they do it right ? The reason I want to capture this event is not because I am asking them to do it, it is because I know they will do it therefore I need the database to know when this happens. If you have a better suggestion please do tell. Wez Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/#findComment-1128426 Share on other sites More sharing options...
Pikachu2000 Posted October 30, 2010 Share Posted October 30, 2010 If it were me, I'd be looking at storing a timestamp in the database to indicate the last acitvity of the user, and checking against that when making the determination if they are still there or not. Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/#findComment-1128429 Share on other sites More sharing options...
wezly Posted October 30, 2010 Author Share Posted October 30, 2010 So say every 3 seconds stamp a time to db.. then when another user trys to connect, the php checks the time on the database relative to current time and if it is say 5 seconds since a stamp then you would assume they are no longer there ?.. that's pretty smart. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/#findComment-1128432 Share on other sites More sharing options...
Pikachu2000 Posted October 30, 2010 Share Posted October 30, 2010 That sounds like an awfully short interval to me. What kind of application is this? Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/#findComment-1128434 Share on other sites More sharing options...
wezly Posted October 30, 2010 Author Share Posted October 30, 2010 It involves flocking video. once it is ready I will drop you a test link http://filmflock.co.uk this has turned in to more of a php, mySQL afair, can you help me out with the mySQL time commands? I have had a quick browse which turned up the Now() function. $query_auto = "INSERT INTO db_time (db_time) VALUE ( NOW() )"; am I looking at the right stuff? Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/#findComment-1128435 Share on other sites More sharing options...
phpfreak Posted January 15, 2011 Share Posted January 15, 2011 I think you should consider an AJAX callback on your Javascript confirmation. You can write a script that detects when the user wants to close the window using window.close() then do a dialog confirmation - if the user clicks "Ok" to close the window, do a quick ajax call to a backend script that simply updates the timestamp in the database. I hope that general idea helps.. Quote Link to comment https://forums.phpfreaks.com/topic/217297-how-do-i-detect-when-a-user-closes-the-browser/#findComment-1159838 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.