PHP ignore_user_abort Function Details and Use Examples

  • 2021-07-09 07:26:07
  • OfStack

ignore_user_abort sets whether disconnecting from the client terminates script execution.

This function returns the previous value (1 Boolean value) set by user-abort.
int ignore_user_abort ([ string $value ] )

Parameter setting description: Optional. If set to true, disconnection from the user is ignored, and if set to false, the script will stop running.
If this parameter is not set, the current setting is returned.

Note: PHP does not detect whether the user has been disconnected until it attempts to send information to the client. Simply using the echo statement does not ensure the message is sent, see the flush () function.

Example-1 1 example of ignore_user_abort (), combined with set_time_limit () function and an infinite loop, the task planning function can be realized.


<?php
ignore_user_abort(true);
 
set_time_limit(0);
echo 'Testing connection handling in PHP';
while (1)
{
    if (connection_status() != CONNECTION_NORMAL) {
 
        break;
 
    }
    // Sleep for 10 seconds
    sleep(10);
}
?>

In the second stage of PHP built-in function research series, PHP function ignore_user_abort is used to realize planning task and continuous process example, and the function and usage of ignore_user_abort () function are discussed through an example of detectable effect.

ignore_user_abort () enables the execution of PHP code when the client is shut down, You can keep PHP process 1 running straight, The so-called scheduled task function and continuous process can be realized, and only the execution script needs to be started. Unless the server such as apache is restarted or there is a script output, the PHP script will be in the execution state directly, which is very practical at first glance, but the cost is a continuous process of PHP execution script, which is very expensive, but it can realize many unexpected functions.

It is described as setting whether disconnecting from the client will terminate the execution of the script.

Attachment: Another introduction

1. Function prototype


int ignore_user_abort ( [bool setting] )

2. Version compatibility

PHP 3 >= 3.0.7, PHP 4, PHP 5

3. Basic usage and examples of functions
1. Basic usage of functions


<?php
ignore_user_abort();
?>

Description: Calling the ignore_user_abort () function declares that the execution of the script will not be terminated even if the client is disconnected.

2. Realize a loop script execution task with set_time_limit () function


<?php
ignore_user_abort();
set_time_limit(0);
$interval=60*15;
do{
// Business performed
}while(true);
?>

Description: Loop every 15 minutes

3. Custom implementation file output and track the execution result of ignore_user_abort () function


<?php
ignore_user_abort ( TRUE );
set_time_limit ( 0 );
$interval = 10;
$stop = 1;
do {
    if( $stop == 10 ) break;
    file_put_contents('liuhui.php',' Current Time: '.time().' Stop: '.$stop);
    $stop++;
    sleep ( $interval );
} while ( true );
?>

Open the liuhui. php file as follows:

Current Time: 1273735029 Stop: 9

The principle is that even if the client terminates the script, it still executes it every 10 seconds, and prints out the current time and termination point, so that the specific effect of ignore_user_abort () function can be tested.

Through an example, it is found that ignore_user_abort () function is very practical, and it is very effective to plan tasks, complete follow-up tasks and continue processes. Please refer to the PHP manual for more instructions. Please pay attention to the next issue of PHP built-in function research series.


Related articles: