PHP programs functions that continue to execute after the task closes the browser

  • 2020-03-31 20:55:41
  • OfStack

Remember this function:
Name of function: ignore_user_abort
This function concatenates or obtains whether a PHP program continues to execute after the use end connection is broken. The default value is to stop execution when the connection is broken. The ignore_user_abort option in the PHP configuration file (php3.ini/php.ini) is where the configuration is. This feature wasn't available until after version 3.0.7 of PHP.
The official explanation: http://cn2.php.net/manual/en/function.ignore-user-abort.php


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

This allows the scheduled task to be implemented, but only if the client accesses the program.
For example, there is no need to wait while generating static pages and collecting. Close the browser.
Example:
 
//test 
set_time_limit(0); 
ignore_user_abort(true); 
$i = 0 ; 
while($i ++ < 200){ 
file_put_contents($i.'.php' , $i); 
sleep(3); 
} 

Implement the PHP scheduled task with the ignore_user_abort function
 
<?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); //We do it every half an hour
} 
?> 

Related articles: