php ignore_user_abort()函数之计划任务实现方法

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()函数 和一个死循环就可以实现计划任务功能.

  1. <?php 
  2.  
  3.  // Ignore user aborts and allow the script 
  4.  
  5.  // to run forever 
  6.  
  7.  ignore_user_abort (true); 
  8.  
  9.  set_time_limit (0); 
  10.  
  11.  echo 'Testing connection handling in PHP' ; 
  12.  
  13.  // Run a pointless loop that sometime 
  14.  
  15.  // hopefully will make us click away from 
  16.  
  17.  // page or click the "Stop" button. 
  18.  
  19.  while(1) 
  20.  
  21.  { 
  22.  
  23.  // Did the connection fail? 
  24.  
  25.  if( connection_status () != CONNECTION_NORMAL ) 
  26.  
  27.  { 
  28.  
  29.  break
  30.  
  31.  } 
  32.  
  33.  // Sleep for 10 seconds 
  34.  
  35.  sleep (10); 
  36.  
  37.  } 
  38.  
  39.  // If this is reached, then the 'break' 
  40.  
  41.  // was triggered from inside the while loop 
  42.  
  43.  // So here we can log, or perform any other tasks 
  44.  
  45.  // we need without actually being dependent on the 
  46.  
  47.  // browser. 
  48.  
  49.  ?> 

实例 1、

关闭浏览器后,程序能继续在后台跑,这种情况下需要用到ignore_user_abort()函数;

  1. ignore_user_abort(true);       //设置客户端断开连接时是否中断脚本的执行 
  2.  
  3.       
  4.  
  5.     set_time_limit(0); 
  6.          $file = '/tmp/ignore_user.txt'
  7.          if(!file_exists($file)) { 
  8.                  file_put_contents($file); 
  9.          } 
  10.          if(!$handle = fopen($file,'a+b')){ 
  11.                  echo "not open file :".$file
  12.                  exit
  13.          } 
  14.          $i=0; 
  15.          while($i<100) { 
  16.                  $time = date("Y-m-d H:i:s",time()); 
  17.                  echo $time.""
  18.                  if(fwrite($handle,$time."")===false) { 
  19.                          echo "not write file:".$file
  20.                          exit
  21.                  } 
  22.                  echo "write file time:".$time.""
  23.                  $i++; 
  24.                  sleep(2); 
  25.          } 
  26.  
  27. fclose($handle); 
加上这段代码,即使你把浏览器关闭后还是能还执行php计划任务哦.
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » php ignore_user_abort()函数之计划任务实现方法

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡