php ignore_user_abort
函数说明(PHP 4中,PHP 5中)
ignore_user_abort 设置与客户机断开是否会终止脚本的执行.
本函数返回 user-abort 设置的之前的值(一个布尔值).
函数定义
int ignore_user_abort ([ string $value ] )
参数 描述
setting 可选.如果设置为 true,则忽略与用户的断开,如果设置为 false,会导致脚本停止运行.
如果未设置该参数,会返回当前的设置.
提示注释
注释:PHP 不会检测到用户是否已断开连接,直到尝试向客户机发送信息为止.简单地使用 echo 语句无法确保信息发送,参阅 flush() 函数.
实例说明
例-1 一个的ignore_user_abort()的例子,配合set_time_limit()函数 和一个死循环就可以实现计划任务功能.
- <?php
- // Ignore user aborts and allow the script
- // to run forever
- ignore_user_abort (true);
- set_time_limit (0);
- echo 'Testing connection handling in PHP' ;
- // Run a pointless loop that sometime
- // hopefully will make us click away from
- // page or click the "Stop" button.
- while(1)
- {
- // Did the connection fail?
- if( connection_status () != CONNECTION_NORMAL )
- {
- break;
- }
- // Sleep for 10 seconds
- sleep (10);
- }
- // If this is reached, then the 'break'
- // was triggered from inside the while loop
- // So here we can log, or perform any other tasks
- // we need without actually being dependent on the
- // browser.
- ?>
实例 1、
关闭浏览器后,程序能继续在后台跑,这种情况下需要用到ignore_user_abort()函数;
- ignore_user_abort(true); //设置客户端断开连接时是否中断脚本的执行
- set_time_limit(0);
- $file = '/tmp/ignore_user.txt';
- if(!file_exists($file)) {
- file_put_contents($file);
- }
- if(!$handle = fopen($file,'a+b')){
- echo "not open file :".$file;
- exit;
- }
- $i=0;
- while($i<100) {
- $time = date("Y-m-d H:i:s",time());
- echo $time."";
- if(fwrite($handle,$time."")===false) {
- echo "not write file:".$file;
- exit;
- }
- echo "write file time:".$time."";
- $i++;
- sleep(2);
- }
- fclose($handle);
加上这段代码,即使你把浏览器关闭后还是能还执行php计划任务哦.
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
波比源码 » php ignore_user_abort()函数之计划任务实现方法
波比源码 » php ignore_user_abort()函数之计划任务实现方法