The php schedule task detects the user connection status

  • 2020-05-16 06:26:57
  • OfStack

Schedule a task
 
ignore_user_abort(); //  The user closes the browser and the program still executes  
set_time_limit(0); //  There is no limit to program running time  
$interval = 3; //  Program cycle interval seconds  

$link = mysql_connect('localhost', 'username', 'paswd'); 
mysql_select_db('test'); 
mysql_query("SET NAMES 'utf8'"); 

do { 
//  The user closes the browser to stop  start 
echo str_repeat(' ', 4069); // php Check the user connection status only on output . 1 some web The server's output_buffering The default value is 4096 character .  In order to ensure the flush() effective ,  Set to 4069. 
ob_flush(); 
flush(); 
//  The user closes the browser to stop  end 

$query = "INSERT INTO `test`.`test_demo` (`title`, `content`) VALUES (' Schedule a task ', '" . date("Y-m-d H:i:s", time()) . "')"; 
mysql_query($query); //  Use write database validator  
sleep($interval); 
} while (true); 

The user connection status cannot be detected without the output php. Even if the browser is closed, the application will continue to run until the apache service is stopped or restarted.
 
//  The user closes the browser to stop  start 
echo str_repeat(' ', 4069); // php Check the user connection status only on output . 1 some web The server's output_buffering The default value is 4096 character .  In order to ensure the flush() effective ,  Set to 4069. 
ob_flush(); 
flush(); 
//  The user closes the browser to stop  end 

Related articles: