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 will exit 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 in the max_execution_time or Apache. conf 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 that the ABORTED and TIMEOUT statuses are valid at the same time. 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.
2. related functions:
Int ignore_user_abort ([bool setting])
This function sets whether a client disconnect shocould cause a script to be aborted. it will return the previous setting and can be called without an argument to not change the current setting and only return the current setting.
Int connection_aborted (void)
Returns TRUE if client disconnected.
Int connection_status (void)
Returns the connection status bitfield.
To regularly update a file, the program runs automatically. two methods are found on the Internet: ignore_user_abort () and crontab.
The ignore_user_abort () function can be used with set_time_limit (0) and sleep ($ interval) to automatically run and update programs. The following is an instance.
The code is as follows:
Ignore_user_abort (); // The PHP script can be executed even if the Client is disconnected (such as the browser is disabled.
Set_time_limit (0); // the execution time is unlimited. the default php execution time is 30 seconds. you can use set_time_limit (0) to limit the execution of the program.
$ Interval = 60*5; // run every 5 minutes
Do {
$ Fp = fopen('test.txt ', 'A ');
Fwrite ($ fp, 'test ');
Fclose ($ fp );
Sleep ($ interval); // wait for 5 minutes
} While (true );
As long as you run the above page, and then turn it off, the program will continue to run.
In Linux, there is a simpler method, that is, the crontab command.
The crontab command schedules the execution of some commands at certain intervals.
Crontab usage: crontab [-e |-l |-r] file name-e: edit task-l: Display task information-r: delete scheduled execution task information
Crontab format:
* *** Command
Command to be run by hour, day, month, and week
Crontab example:
*/5 * lynx http://www.bitsCN.com
Access www.bitsCN.com every 5 minutes
0 8 *** lynx http://www.bitsCN.com
Visit www.bitsCN.com at every morning
0 10 6*1-5 lynx http://www.bitsCN.com
Visit www.bitsCN.com on the 6th of each month and from Monday to Friday at AM.
0 5 7 8 * lynx http://www.bitsCN.com
Visit www.bitsCN.com at, January 1, August 7
The above special meanings:
"*" Indicates all numbers in the value range, "/" indicates each meaning, and "*/5" indicates every five units, "-" represents a number from a number to a number, "," separate several discrete numbers.
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