1

Possible Duplicate:
Detect browser connection closed in PHP

There are times where I must use something like this:

set_time_limit(0);

// User is uploading some large data
// ...

// Done uploading, from now on processing must be finished, no matter what
ignore_user_abort(true);

// Process the uploaded data...
while (reading the data) {
    // ...
}

The problem is, there are cases where I need to know when user cancel/close the browser window (while I'm still processing the data), so that I can do some db sync/cleanup

I read about register_shutdown_function, but it seems not the way to go, I need a reliable indicator that tells that user is gone, not just a shutdown notification, ie.

// Process the uploaded data...
while (reading the data) {
    if (user_closed_window == true) {
        CleanUp();
        break;
    }
}
Community
  • 1
  • 1
TheDude
  • 3,045
  • 4
  • 46
  • 95
  • use `if (connection_status()==1){` or `if (connection_aborted()) {`. check [`connection handling`](http://www.php.net/manual/en/features.connection-handling.php) – air4x Oct 27 '12 at 03:03

1 Answers1

0

That seems kind of impossible to do with PHP. But I think you can do this:

In some way you don't commit your operation, you just do the operation saving it on a buffer for example. Next you check if the user cancelled/closed the window. This can be done with AJAX, you start the operation via XHTTP request and show an other page to the user (with a cancel button perhaps). I believe that you can detect if the user closed the window via JS+AJAX. If your operation has ended and the user didn't closed the window/cancelled the operation, then you commit the changes. If not, you simply discard.

If your operation is huge, you can divide in batch it and check if the user has cancelled/closed between the jobs.

I think that this is a feasible way to do that in PHP. Hope it helps.

Murilo Vasconcelos
  • 4,677
  • 24
  • 27