Hi
I have updated the database when browser is closed,But i have a small problem.i.e. The database is updating when the page is refreshing also.
I need that the Database should be updated on when the browser is closed(Not updated when page is refreshed).

Recommended Answers

All 2 Replies

"The onLoad and onUnload events are triggered when the user enters or leaves the page." -- w3schools

A refresh is leaving a page and then entering the same once again. I don't think there is a way to see if the same page will be loading again, or if the browser is closing.

onbeforeunload only appears to be working in IE en FF.

If a browser reloads a new HTTP request is sent. So what you can do is wait a few seconds for the next HTTP Request.

eg:

On the PHP page that updates the DB do a

sleep(15);

This way the script waits for 15 secs. Then check if the user session has been updated after the script went to sleep. If so, then a new request was made.

You could probably take that down to 10 or even 5 secs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.