Jump to content

How do I detect when a user closes the browser ?


wezly

Recommended Posts

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.

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • 2 months later...

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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.