Home > Backend Development > PHP Tutorial > PHP计划任务之关闭浏览器后仍然继续执行的函数_php技巧

PHP计划任务之关闭浏览器后仍然继续执行的函数_php技巧

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-17 09:24:17
Original
1205 people have browsed it

备忘一下这个函数:
函数名称:ignore_user_abort
本函数配置或取得使用端连接中断后,PHP 程序是否仍继续执行。默认值为中断连接后就停止执行。在 PHP 配置文件中 (php3.ini/php.ini) 的 ignore_user_abort 选项就是配置处。本功能在 PHP 3.0.7 版之后才开始提供。
官方说明:http://cn2.php.net/manual/en/function.ignore-user-abort.php


使用方法:

复制代码 代码如下:

ignore_user_abort(true); //即使Client断开(如关掉浏览器),PHP脚本也可以继续执行.

这样就可以实现计划任务效果了.但是还是要客户端访问程序才行.
比如说在生成静态页面、采集的时候,不需要在等待了。关掉浏览器吧。
例子:
复制代码 代码如下:

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

用ignore_user_abort函数实现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")."\n\r";
  fwrite($fp,$str);
  fclose($fp);
  sleep(5); //半小时执行一次
}
?>
Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template