yes, the browser evoke unload event when leave the page (back, forward, or
close). you can send a message via ajax, but it may not been received by the
server successful, (when close the browser)

I think the best way is maintaining a timestamp as michael & paul say, and
clear the 'junk' timely.

there is another way if you need to get some INFO when user leaves this page
(such as leave time...):
 save the INFO into cache
 set time interval do the follow:
  1. load INFO from cache
  2. send the INFO via ajax and wait the server confirm it has received the
  3. if the server response is ture, delete the INFO or send the INFO again
a while later
by this way, even users closed their browser, but the next time they open
your web page, they will send the cached INFO too.

On Wed, Jul 1, 2009 at 10:59 AM, Paul M Foster <>wrote:

> On Tue, Jun 30, 2009 at 06:38:19PM -0700, Mary Anderson wrote:
> > Hi all,
> >
> >   I have a php application for which I have a page which creates
> > temporary junk and puts it into a persistent store  (in this case a
> > postgres database, but that is beside the point.)
> >
> >   I have a Save button which puts the stuff I really want into the
> > persistent store and cleans up the temporary junk.  I have a Cancel
> > button which only cleans up the temporary junk.
> >
> >    I would really like to have the cleanup code run anytime a user
> > leaves the page without hitting the Save button.  Is there anyway to do
> > this?  I.e. have php code called conditionally on exiting the page?
> If the user hits the "Back" button or exits via clicking on some link on
> the page, there's no reasonable way to do this (Michael's cron versions
> excepted). Because the HTTP protocol has no effective memory, there's no
> way for it to know that the user exited without saving. You can do
> various tricky things like set a $_SESSION variable, and then have it
> checked at the top of each page where the user could go, and check and
> clean up if the variable has a certain value. But that's pretty tedious.
> You could execute the whole thing as part of a class, and put a check in
> the constructor of the class (where it will be called on every page)
> which cleans up if necessary.
> In other words, there are ways to do this, but they are tedious. The
> PHP/HTTP execution engines have no way of doing it on their own.
> (Oh, there may be a way to do this with Javascript. I'm not good with
> Javascript, but maybe there's some "OnExit" event which you could use to
> cause a Javascript function to do an AJAX call to a PHP script which
> cleans up. I dunno.)
> Paul
> --
> Paul M. Foster
> --
> PHP General Mailing List (
> To unsubscribe, visit:

Best Regards!
Wen Dong

Reply via email to