ThinkPHP5 browser is closed and continues to execute the php script

Currently, scheduled tasks are a necessary requirement for a website, such as regularly publishing articles on Mipu blog , regularly cleaning up spam messages, regularly sending emails and sending text message reminders by Mipu agents , etc.

Most of today's websites are developed using PHP dynamic language. For PHP, there is no concept of AppServer such as Java and .Net. The http protocol is a stateless protocol. PHP can only be triggered by the user and called. It will automatically exit the memory. Without resident memory, it is generally impossible to implement scheduled tasks with long connections.

So, if you have to implement scheduled tasks in PHP, what are the methods? This article on Mipu Blog summarizes several solutions.

1. Simple, direct and reckless type

<?php
    ignore\_user\_abort();                // 关掉浏览器,PHP脚本也可以继续执行.
    set\_time\_limit(0);                  // 通过set\_time\_limit(0)可以让程序无限制的执行下去
    ini\_set('memory\_limit','1024M');    // 设置内存限制
    $interval = 60\*30;                  // 每隔半小时运行
    do{
      // ToDo mimvp task
      sleep($interval);                 // 等待5分钟
    }
    while(true);
?>

**Disadvantages:** Once started, it cannot be controlled unless the PHP host is terminated. This method is generally not recommended unless you are a hacker.

2. Simple and controllable

  
<?php
    return 1;
?>
 
 
cron.php
    
ignore\_user\_abort();    // 关掉浏览器,PHP脚本也可以继续执行.
set\_time\_limit(0);      // 通过set\_time\_limit(0)可以让程序无限制的执行下去
$interval = 60\*30;      // 每隔半小时运行
do{
  $run \= include 'config.php';      // 通过配置文件,来实现停止程序
  if(!$run) die('process abort');
    
  //ToDo mimvp task
  sleep($interval);     // 等待5分钟
}
while(true);

To stop the program by changing the config.php return 0 ,, a feasible way is to interact with the config.php file and a special form, and set some variables through the HTML page for configuration.

**Disadvantages:** It occupies system resources, and if it runs for a long time, there will be some unexpected hidden dangers. For example, memory management, someone maliciously changing the configuration file, etc.

3. Simple improved type

<?php
    $time \= 15;
    $url \= "http://".$\_SERVER\['HTTP\_HOST'\].$\_SERVER\['REQUEST\_URI'\];
 
    //  ToDo mimvp task
 
    sleep($time);
    file\_get\_contents($url);
?>

The php script sleeps for a while and continues to execute by accessing itself, just like a relay race...

This will ensure that the execution time of each PHP script will not be too long, and there will be no time_outrestrictions.

Because each cycle of the php file is executed independently, this method avoids time_outthe limitation. But it is best to add the control code. cofig.php as above, so that the process can be terminated.

4. Organize by yourself

ignore\_user\_abort();
//即使Client断开(如关掉浏览器),PHP脚本也可以继续执行.
set\_time\_limit(0);
//执行时间为无限制,php默认的执行时间是30秒,通过set\_time\_limit(0)可以让程序无限制的执行下去
$time = 15;
    $url \= "http://".$\_SERVER\['HTTP\_HOST'\].$\_SERVER\['REQUEST\_URI'\];
 
    //  ToDo mimvp task
 
    sleep($time);
    file\_get\_contents($url);

This article is reproduced from https://www.cnblogs.com/Im-Victor/p/15901857.html . If there is any infringement, please contact us for deletion.

Guess you like

Origin blog.csdn.net/qq_35606400/article/details/130211679