Home > Backend Development > PHP Tutorial > PHP scheduled task PHP scheduled task function that continues to execute after closing the browser

PHP scheduled task PHP scheduled task function that continues to execute after closing the browser

WBOY
Release: 2016-07-29 08:43:22
Original
1330 people have browsed it

Remember this function:
Function name: ignore_user_abort
This function configures or obtains whether the PHP program will continue to execute after the user connection is interrupted. The default value is to stop execution after disconnecting. The ignore_user_abort option in the PHP configuration file (php3.ini/php.ini) is the configuration location. This feature is only available after PHP version 3.0.7.
Official description: http://cn2.php.net/manual/en/function.ignore-user-abort.php
How to use:

Copy the code The code is as follows:


ignore_user_abort(true); //Even if the Client is disconnected (such as closing the browser), the PHP script can continue to execute.


In this way, the scheduled task effect can be achieved. But the client still needs to access the program.
For example, when generating a static page , when collecting, there is no need to wait. Close the browser.
Example:

Copy code The code is as follows:


//test
set_time_limit(0);
ignore_user_abort(true);
$i = 0;
while($i ++ < 200){
file_put_contents($i.'.php' , $i);
sleep(3);
}


Use ignore_user_abort function to implement php scheduled tasks

Copy the code The code is as follows:


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); //Execute once every half hour
}
?>

The above introduces the PHP scheduled task. The PHP scheduled task continues to execute functions after closing the browser, including the content of the PHP scheduled task. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template