Yoshiyahu
Yoshiyahu

Reputation: 2158

What happens to canceled requests to a PHP page?

When a long-running PHP file is executing, and the user cancels the page request in their browser midway, is the rest of the script ran on the server?

Upvotes: 10

Views: 2285

Answers (3)

Vince Mongiardo
Vince Mongiardo

Reputation: 59

For me (using XAMPP on an older computer), cancelling the web page in the middle of an operation meant to process one million primary records, with a couple of other tables being updated in the process, the MySQL database operations kept going, which tells me that the PHP script kept running on the server. By "cancelling," I mean clicking the stop button on the tab, but not closing the tab altogether. BTW: I had established set_time_limit() to 0 to keep the script from timing out before completing in absence of cancelling.

Upvotes: 0

nsanders
nsanders

Reputation: 12646

PHP normally terminates script execution once it realizes that the connection is closed:

PHP will not detect that the user has aborted the connection until an attempt is made to send information to the client. Simply using an echo statement does not guarantee that information is sent, see flush().

You can keep your script running using ignore_user_abort().

Also, there is a default time limit for which scripts are allowed to run. You may want to override that using set_time_limit().

Upvotes: 11

Markel Mairs
Markel Mairs

Reputation: 741

I tested this once on a long running process and discovered that the script will continue to run once it has not exceeded the maximum time to execute.

Upvotes: 1

Related Questions