The phpignore_user_abort function indicates whether the script execution will be terminated if the ignore_user_abort setting is disconnected from the client in PHP4 and PHP5. This function returns the previous value (a Boolean value) set by user-abort ).
Php ignore_user_abort
Function description (in PHP 4 and in PHP 5)
Ignore_user_abort sets whether the script execution will be terminated when the client is disconnected.
This function returns the previous value (a Boolean value) set by user-abort ).
Function definition
Int ignore_user_abort ([string $ value])
Parameter description
Setting is 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.
Prompt comment
Note: PHP does not detect whether the user is disconnected until it attempts to send messages to the client. simply use the echo statement to ensure that messages are sent. see flush () function.
Instance description
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
-
- // Browser.
-
- ?>
Instance 1,
After the browser is closed, the program can continue running in the background. in this case, the ignore_user_abort () function is required;
- Ignore_user_abort (true); // Sets whether the script execution is interrupted when the client is disconnected.
-
-
- Set_time_limit (0 );
- $ File = '/tmp/ignore_user.txt ';
- If (! File_exists ($ file )){
- File_put_contents ($ file );
- }
- If (! $ Handle = fopen ($ file, 'A + B ')){
- Echo "not open file:". $ file;
- Exit;
- }
- $ I = 0;
- While ($ I <100 ){
- $ Time = date ("Y-m-d H: I: s", time ());
- Echo $ time. "\ n ";
- If (fwrite ($ handle, $ time. "\ n") === false ){
- Echo "not write file:". $ file;
- Exit;
- }
- Echo "write file time:". $ time. "\ n ";
- $ I ++;
- Sleep (2 );
- }
-
- Fclose ($ handle );
With this code, you can still execute php scheduled tasks even after you close your browser.