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

PHP scheduled task function that continues to execute after closing the browser_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:36:02950browse

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


Usage:

Copy 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. However, the client still needs to access the program.
For example, when generating static pages and collecting data, 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 code Code 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
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322222.htmlTechArticleRemember this function: Function name: ignore_user_abort This function configures or obtains whether the PHP program will Execution continues. The default value is to stop execution after disconnecting...
Statement:
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