View Full Version : on close

03-12-2007, 10:15 PM
In php is there a method for that can be used when a user closes the browser?
I want to set a variable when the user closes the window


03-12-2007, 10:28 PM
PHP does not interact with the browser except through the HTTP request area. Javascript has an onclose() function, which isn't reliable as the browser can choose to ignore it (most popup blockers block this event).

03-12-2007, 10:30 PM
No. PHP runs on the server, not the client. Your variable has no meaning by the time the client is using the page (unless it's session or application scope).

Javascript can catch the page being unloaded, but that's about it. And even that is iffy with tabbed browsers since you don't know if the entire browser is closed or just a tab.
Web applications are best coded with the stateless nature of http in mind. Do your cleanup on session timeout, if you can, or code cleanup periodically in a stateless manner.
Amazon has some great examples of how they handle things like stuff left in the shopping cart and whatnot.

03-12-2007, 10:33 PM
thanks for that!