Briefly introduce some relevant knowledge according to the PHP manual:
1. Connection processing:
Within PHP, the system maintains a connection state with three possible conditions:
0-normal (Normal)
1-aborted (Exception exit)
2-timeout (Timeout)
The connection is valid when the PHP script normally runs in normal state. When a remote client disconnects, the flag for the aborted state is opened. The interruption of a remote client connection is usually caused by the user clicking the STOP button. When the connection time exceeds the PHP timeline, the TIMEOUT status tag is opened.
You can determine whether the script needs to exit when the client disconnects. Sometimes it can be handy to have a script run completely, even if there is no remote browser to accept the output of the script. The default scenario is that the script exits when a remote client connection is interrupted. The process can be controlled by the php.ini Ignore_user_abort or by the corresponding "Php_value ignore_user_abort" and the Ignore_user_abort () functions in the Apache. conf settings. If PHP is not told to ignore user interrupts, the script will be interrupted unless the shutdown trigger function is set by Register_shutdown_function (). With this shutdown trigger function, when the remote user clicks the STOP button and the script tries to output the data again, PHP detects that the connection has been interrupted and invokes the shutdown trigger function.
The script may also be interrupted by a built-in script timer. The default time-out limit is 30 seconds. This value can be changed by setting the corresponding "Php_value max_execution_time" parameter or the Set_time_limit () function in the php.ini max_execution_time or Apache. conf settings. When the counter times out, the script will be similar to the above connection interrupt exit, the previously registered shutdown trigger function will be executed at this time. In the shutdown trigger function, the connection_status () function can be called to check whether the timeout causes the shutdown trigger function to be invoked. If the timeout causes a call to close the triggering function, the function returns 2.
One thing to note is that the aborted and TIMEOUT states can be valid at the same time. This is possible when you tell PHP to ignore the user's exit operation. PHP will still be aware that the user has disconnected the connection but the script is still running. If the time limit is run, the script will be exited, and the set off trigger function will be executed. At this point the function connection_status () is found to return 3.
2. Related functions:
int Ignore_user_abort ([bool setting])
This function sets whether a client disconnect should cause a script to be aborted. It'll return the previous setting and can is called without a argument to don't change the current setting and only retur n the current setting.
int connection_aborted (void)
Returns TRUE if client disconnected.
int connection_status (void)
Returns the connection status Bitfield.
In order to regularly update a file, need to run the program automatically, from the internet found two ways: Ignore_user_abort () and crontab
Ignore_user_abort () function with Set_time_limit (0) and sleep ($interval) to implement the program automatically run updates, the following is an example
Copy Code code as follows:
Ignore_user_abort (); Even if the client disconnects (such as turning off the browser), the PHP script can continue to execute.
Set_time_limit (0); Execution time is unlimited, php default execution time is 30 seconds, through Set_time_limit (0) can allow the program to execute indefinitely
$interval =60*5; Run every 5 minutes
do{
$fp = fopen (' test.txt ', ' a ');
Fwrite ($fp, ' test ');
Fclose ($FP);
Sleep ($interval); Wait 5 minutes
}while (TRUE);
Just run the top page and then turn it off, and the program will run forever.
There's a simpler way to do that with Linux, the crontab command.
The function of the crontab command is to schedule the execution of some commands at certain intervals.
Crontab usage: crontab [e | l |-r] File name-e: Edit Task-L: Show task Information-r: Delete timed execution task information
Crontab format:
* * * * Command
Time-Sharing Week the command to run
Examples of crontab:
*/5 * * * * Lynx Http://www.jb51.net
Visit Www.jb51.net every 5 minutes
0 8 * * * Lynx Http://www.jb51.net
Visit www.jb51.net every morning at 8.
0 6 * 1-5 Lynx Http://www.jb51.net
6th a month, and a 10-point visit to the morning of Monday to Friday each week www.jb51.net
0 5 7 8 * Lynx Http://www.jb51.net
August 7 Morning 5 visit to Www.jb51.net.
A few special meanings above:
"*" represents the number in the range of all values, "/" for each meaning, "*/5" means every 5 units, "-" represents a number from a certain number, "," separate several discrete numbers.