Current Location: Home> Latest Articles> Use ignore_user_abort in long-running tasks to avoid PHP script timeouts

Use ignore_user_abort in long-running tasks to avoid PHP script timeouts

gitbox 2025-05-29

When using PHP to perform long-term tasks, we often encounter a situation where the user closes the browser or actively disconnects the request, and the PHP script will be aborted. This is very fatal for scripts that require long-term running or continuous processing of the backend. So, how to prevent the script from being aborted when the user is disconnected? The answer is to use the ignore_user_abort() function.

What is ignore_user_abort?

ignore_user_abort() is a function provided by PHP to set whether the script continues to execute when the client disconnects.

 ignore_user_abort(true);

When a true parameter is passed, the PHP script will continue to execute even if the user closes the browser until the execution is completed or timed out/actively exits.

Application scenarios of ignore_user_abort

  • Handle asynchronous tasks

  • Background queue processing

  • Timed task simulation execution

  • File generation or big data export

  • Backstage operations that need to maintain state consistency

To give a practical example, if you are developing a system backup function, after the user clicks "Start Backup", the script needs to run for more than ten minutes to complete. If the user closes the browser, the script will be aborted by default and the backup task will fail. And through ignore_user_abort(true) , even if the user leaves the page, the script will continue to be executed on the server until the backup is completed.

How to use ignore_user_abort

Here is a typical long task processing code example:

 <?php
ignore_user_abort(true); // Continue execution even if the user breaks the connection
set_time_limit(0); // Cancel script execution time limit

file_put_contents('log.txt', "Mission start time:" . date('Y-m-d H:i:s') . "\n", FILE_APPEND);

// Simulate a long-term task
for ($i = 1; $i <= 10; $i++) {
    file_put_contents('log.txt', "1. {$i} Step-by-step execution...\n", FILE_APPEND);
    sleep(5); // Time-consuming for each step of simulation
}

file_put_contents('log.txt', "Task completion time:" . date('Y-m-d H:i:s') . "\n", FILE_APPEND);
?>

You can call this script through curl or Ajax requests, for example:

 fetch("https://gitbox.net/run-task.php");

Even if the user closes the browser after sending the request, the script will be executed in full.

Check connection status

If you want to do some logical processing after the user is disconnected, such as writing logs or aborting certain behaviors, you can use the connection_aborted() function to detect:

 if (connection_aborted()) {
    file_put_contents('log.txt', "User connection has been interrupted\n", FILE_APPEND);
}

Used with ignore_user_abort(true) , you can more flexibly handle interrupts and non-interruptions.

Things to note

  • Using ignore_user_abort() does not mean that you can ignore all exceptions, interrupts may also come from the server side (such as Nginx, Apache configuration) or the PHP script itself (such as memory limit).

  • It is recommended to use set_time_limit(0) at the same time to cancel the maximum execution time limit.

  • If the script outputs data to the client, try to use ob_start() to enable buffering before logic to avoid connection problems caused by output behavior.

Summarize

When handling long tasks in PHP, using ignore_user_abort(true) is an effective way to ensure that the script is not interrupted by the user in the middle. It can ensure that tasks are fully executed on the server side and improve the stability and reliability of the system. This is a very practical trick for background scripts involving tasks such as backup, push, export, etc.