How to implement the ignore_user_abort function for task planning in php _ php skills

Source: Internet
Author: User
This article mainly introduces the implementation of the ignore_user_abort function of php scheduled tasks, and analyzes the implementation of the ignore_user_abort function of php scheduled tasks in the form of examples, the usage of the ignore_user_abort function is described in detail. For more information, see the following example to describe how to implement the ignore_user_abort function for php scheduled tasks. Share it with you for your reference. The specific analysis is as follows:

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 the messages are sent. see the flush () function. the code is as follows:

The code is as follows:

<? Php
Ignore_user_abort (true );
Set_time_limit (0 );

While (1 ){
$ Fp = fopen('time_task.txt ', "a + ");
$ Str = date ("Y-m-d h: I: s"). "nr ";
Fwrite ($ fp, $ str );
Fclose ($ fp );
Sleep (5); // run once every 30 minutes
}
?>

Definition and usage

The ignore_user_abort () function sets whether the script execution is terminated when the client is disconnected. This function returns the value (a Boolean value) set before user-abort ).

Syntax: ignore_user_abort (setting)

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.

Tips and comments

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 the messages are sent. see flush () function.

Below are additional information

Here is a simple example:

<? Php // php scheduled task ignore_user_abort (); // Set whether the execution of the script will be terminated when the function is disconnected from the client. set_time_limit (0 ); // set the execution time of a script to an infinite length $ interval = 30; do {$ fp = fopen('text3.txt ', 'A'); fwrite ($ fp, 'test '); fclose ($ fp); sleep ($ interval); // Function delay code execution several seconds} while (true);?>

First, run the program, and then close the page. when the program is still running, testwill fill in the text3.txt file every other 30 seconds.
Finally, I will introduce the related knowledge in the php manual:

1. connection processing:
In PHP, the system maintains the connection status. There are three possible states:

The code is as follows:


0-NORMAL (NORMAL)
1-ABORTED (exit unexpectedly)
2-TIMEOUT (TIMEOUT)

When the PHP script runs normally, the connection is valid. When the remote client is disconnected, the ABORTED status mark will be opened. The interruption of remote client connection is usually caused by the user clicking the STOP button. When the connection time exceeds the PHP time limit, the TIMEOUT status mark will be opened.

You can determine whether the script needs to exit when the client terminates the connection. Sometimes it is much more convenient to run the script completely, even if the script output is not accepted by a remote browser. By default, the script exits when the remote client connection is interrupted. This process can be controlled by php. ini's ignore_user_abort or by the "php_value ignore_user_abort" and ignore_user_abort () functions in Apache. conf settings. If PHP is not told to ignore user interruptions, the script will be interrupted unless the trigger function is disabled through register_shutdown_function. When a remote user clicks the STOP button and the script tries to output data again, PHP will detect that the connection has been interrupted and call to disable the trigger function.

The script may also be interrupted by the built-in script timer. The default timeout limit is 30 seconds. This value can be changed by setting the "php_value max_execution_time" parameter corresponding to the max_execution_time setting of php. ini or the set_time_limit () function. When the counter times out, the script will exit when the above connection is interrupted, and the previously registered closed trigger function will also be executed at this time. In this function, you can call the connection_status () function to check whether timeout has caused the function to be called. If timeout causes function call to be disabled, the function returns 2.

Note: the ABORTED and TIMEOUT statuses can be both valid. It is possible to tell PHP to ignore the user's exit operation. PHP will still note that the user has interrupted the connection but the script is still running. If the running time limit is reached, the script will be exited, and the set function to close and trigger will also be executed. The connection_status () function returns 3.

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.