Ignore_user_abort sets whether the script execution is terminated when the client is disconnected. This function returns the previous value (a Boolean value) set by user-abort ). Intignore_user_abort ([string $ value]) parameter description setting
Ignore_user_abort sets whether the script execution is terminated when the client is disconnected. This function returns the previous value (a Boolean value) set by user-abort ). Int ignore_user_abort ([string $ value]) parameter description setting
Ignore_user_abort sets whether the script execution is terminated when the client is disconnected.
This function returns the previous value (a Boolean value) set by user-abort ).
Int ignore_user_abort ([string $ value])
| Parameters |
Description |
| Setting |
Optional. If it is set to true, the disconnection from the user is ignored. If it is set to false, the script stops running.
If this parameter is not set, the current setting is returned.
|
Note: PHP does not detect whether the user is disconnected until it attempts to send messages to the client. Simply using the echo statement cannot ensure message sending. For more information, see flush.
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 |
Example-1: An ignore_user_abort () example can be used with the set_time_limit () function and an endless loop to implement the task scheduling function.
// Ignore user aborts and allow the script
// to run forever
ignore_user_abort (true);
set_time_limit (0);
echo 'Testing connection handling in PHP' ;
// Run a pointless loop that sometime
// hopefully will make us click away from
// page or click the "Stop" button.
while(1)
{
// Did the connection fail?
if( connection_status () != CONNECTION_NORMAL )
{
break;
}
// Sleep for 10 seconds
sleep (10);
}
// If this is reached, then the 'break'
// was triggered from inside the while loop
// So here we can log, or perform any other tasks
// we need without actually being dependent on the
// browser.
?>
|
The second phase of the PHP built-in Function Research Series uses the PHP function ignore_user_abort to implement scheduled tasks and continuous process instances. It also discusses the functions and usage of the ignore_user_abort () function through an instance that can detect the results.
Ignore_user_abort () can be used to execute PHP code after the client is closed. It can keep the PHP process running and implement the so-called scheduled task function and continuous process. You only need to enable the execution script, unless the apache server is restarted or the script has output, the PHP script will remain in the running state. It is very useful at first glance, but the cost is a continuous process of executing the script in PHP, large overhead, but can achieve many unexpected achievements.
It is described as setting whether to terminate the script execution when the client is disconnected.
I. function prototype
?
|
1 |
int ignore_user_abort ( [bool setting] )
|
2. Version compatibility
PHP 3> = 3.0.7, PHP 4, PHP 5
III. Basic Function usage and Examples
1. Basic Function usage
?
|
1
2
3 |
ignore_user_abort();
?>
|
Note: Call the ignore_user_abort () function to declare that the script is not terminated even if the client is disconnected.
2. Use the set_time_limit () function to execute a cyclic script.
?
|
1
2
3
4
5
6
7
8 |
ignore_user_abort();
set_time_limit(0);
$interval=60*15;
do{
// Executed business
}while(true);
?>
|
Note: It is executed cyclically every 15 minutes.
3. output the custom implementation file and track the execution result of the ignore_user_abort () function.
?
|
1
2
3
4
5
6
7
8
9
10
11
12 |
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. The file content is as follows:
Current Time: 1273735029 Stop: 9
The principle is that, even if the client terminates the script, it still runs once every 10 seconds and prints the current time and end point, so that the specific effect of the ignore_user_abort () function can be tested.
It is found through the instance that the ignore_user_abort () function is very practical, and it is very effective to implement the scheduled task, complete the subsequent task, and continue the process. For more information, see the PHP manual. Please pay attention to the next PHP built-in Function Research Series.
Http://www.cnblogs.com/wgw8299/articles/2170092.html