Is it possible to link ignore_user_abort() with a shutdown function that only executes when the script is aborted?

When using ignore_user_abort() in PHP, the script will continue to run even if the user aborts the connection. To link ignore_user_abort() with a shutdown function that only executes when the script is aborted, you can set a flag when the shutdown function is registered and check this flag within the shutdown function to determine if the script was aborted.

<?php

// Set a flag to check if the script was aborted
$aborted = false;

// Register the shutdown function
register_shutdown_function(function() {
    global $aborted;
    
    if ($aborted) {
        // Execute shutdown code only if the script was aborted
        echo "Script was aborted!";
    }
});

// Ignore user abort
ignore_user_abort(true);

// Set the flag if the connection was aborted
if (connection_aborted()) {
    $aborted = true;
}

// Your script code here

?>