php专区

 首页 > php专区 > PHP应用 > php函数大全 > php ignore_user_abort()函数之计划任务实现方法 - ph

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

分享到:
【字体:
导读:
          php ignore_user_abort函数说明PHP 4中,PHP 5中)ignore_user_abort 设置与客户机断开是否会终止脚本的执行。本函数返回 user-abort 设置的之前的值(一个布尔值)。...

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

实例 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."n"
  18.                  if(fwrite($handle,$time."n")===false) { 
  19.                          echo "not write file:".$file
  20.                          exit
  21.                  } 
  22.                  echo "write file time:".$time."n"
  23.                  $i++; 
  24.                  sleep(2); 
  25.          } 
  26.  
  27. fclose($handle); 
加上这段代码,即使你把浏览器关闭后还是能还执行php计划任务哦.
分享到:
php中拷贝构造函数、赋值运算符重载详解 ...
php中拷贝构造函数、赋值运算符重载详解 本文章详细的介绍了关于php中拷贝构造函数、赋值运算符重载详解,有需要了解的同学可参考一下下哦. 对象的赋值与复制: 赋值:通过“ = ”运算符重载 实例代码如下: User a(10),b;  b = a;  复制:调用复制构造函数 User b...
PHP 函数之自定义函数介绍 - php函数
PHP 函数之自定义函数介绍 在 PHP 中,提供了超过 700 个内建的函数. PHP 的函数分为用户自定义函数和系统内置函数.内置函数可以直接使用,用户自定义函数需要使用关键字 function 来定义. 自定义函数 函数(function),可以看着是为实现某个功能的独立的程序语句集合.我们将某个功能写成一...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……