首先要介绍一个函数ignore_user_abort()此函数只要功能是与客户机断开是否会终止脚本的执行。

听说,很多高手全是用什么CRON来做计划任务的功能,目前还没有研究,等研究出来,再分享,今天先分享一个最初级的,希望对大家有所帮助。

那么,就用一个死循环配合set_time_limit()函数来实现一个简单的计划任务:

1
2
3
4
5
6
7
<a rel="noopener noreferrer nofollow" href="http://www.php.net/ignore_user_abort" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">ignore_user_abort</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//关掉浏览器,PHP脚本也可以继续执行.  </span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/set_time_limit" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">set_time_limit</span></a><span style="color: #009900">(</span><span style="color: #cc66cc">0</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">// 通过set_time_limit(0)可以让程序无限制的执行下去  </span>
<span style="color: #000088">$interval</span><span style="color: #339933">=</span><span style="color: #cc66cc">60</span><span style="color: #339933">*</span><span style="color: #cc66cc">30</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">// 每隔半小时运行  </span>
<span style="color: #b1b100">do</span><span style="color: #009900">{</span>  
<span style="color: #b1b100">echo</span> <span style="color: #009900">(</span><span style="color: #0000ff">"www.chhua.com"</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//这里是你要执行的代码      </span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/sleep" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">sleep</span></a><span style="color: #009900">(</span><span style="color: #000088">$interval</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">// 等待5分钟  </span>
<span style="color: #009900">}</span><span style="color: #b1b100">while</span><span style="color: #009900">(</span><span style="color: #009900;font-weight: bold">true</span><span style="color: #009900">)</span><span style="color: #339933">;</span>