搜索
首页php教程php手册PHP 函数 ignore

ignore_user_abort 设置与客户机断开是否会终止脚本的执行。 本函数返回 user-abort 设置的之前的值(一个布尔值)。 int ignore_user_abort ([ string $value ] ) 参数 描述 setting 可

ignore_user_abort 设置与客户机断开是否会终止脚本的执行。

  本函数返回 user-abort 设置的之前的值(一个布尔值)。


int ignore_user_abort ([ string $value ] )

  

参数 描述
setting 可选。如果设置为 true,则忽略与用户的断开,如果设置为 false,会导致脚本停止运行。
参数 描述
setting 可选。如果设置为 true,则忽略与用户的断开,如果设置为 false,会导致脚本停止运行。
如果未设置该参数,会返回当前的设置。
如果未设置该参数,会返回当前的设置。

 注释:PHP 不会检测到用户是否已断开连接,直到尝试向客户机发送信息为止。简单地使用 echo 语句无法确保信息发送,参阅 flush() 函数。


?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

例-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.

  ?>

12

3

45

67

89

10

11
12
13

14

1516

17

18

19

20

1

int ignore_user_abort ( [bool setting] )

2122

23

24
25

26


例-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);

1

2

3

<?php

ignore_user_abort();

?>

  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.  ?>
  PHP内置函数研究系列第二期,采用PHP函数ignore_user_abort实现计划任务与持续进程实例,并通过一个可检测效果的实例讨论ignore_user_abort()函数的作用与用法。ignore_user_abort() 可以实现当客户端关闭后仍然可以执行PHP代码,可保持PHP进程一直在执行,可实现所谓的计划任务功能与持续进程,只需要开启执行脚本,除非 apache等服务器重启或有脚本有输出,该PHP脚本将一直处于执行的状态,初看很实用,不过代价是一个PHP执行脚本的持续进程,开销很大,但却可以 实现很多意想不到的功 能。其描述为设置与客户机断开是否会终止脚本的执行。一,函数原型 ?
1 int ignore_user_abort ( [bool setting] )
  二,版本兼容 PHP 3 >= 3.0.7, PHP 4, PHP 5三,函数基础用法与实例 1,函数基础用法?
123 <?php ignore_user_abort();?>

说明:调用ignore_user_abort()函数声明即使客户机断开不终止脚本的执行。


2,结合set_time_limit()函数实现一个循环脚本执行任务

?

1

2

3

4

5

6

7

8

<?php

ignore_user_abort();

set_time_limit(0);

$interval=60*15;

do{

//执行的业务

}while(true);

?>

12

3

4


56

7

8

<?php

ignore_user_abort();

set_time_limit(0);

1

2

3

4

5

6

7

8

9

10

11

12

<?php

ignore_user_abort ( TRUE );

set_time_limit ( 0 );

$interval = 10;

$stop = 1;

do {

    if( $stop == 10 ) break;

    file_put_contents('liuhui.php',' Current Time: '.time().' Stop: '.$stop);

    $stop ;

    sleep ( $interval );

} while ( true );

?>

$interval=60*15;do{

//执行的业务

}while(true);

?>

 说明:每隔15分钟循环执行

3,自定义实现文件输出并跟踪ignore_user_abort()函数的执行结果?
123456789101112 <?php ignore_user_abort ( TRUE );set_time_limit ( 0 );$interval = 10;$stop = 1;do {    if( $stop == 10 ) break;    file_put_contents('liuhui.php',' Current Time: '.time().' Stop: '.$stop);    $stop ;    sleep ( $interval );} while ( true );?>
打开liuhui.php文件,文件内容如下: Current Time: 1273735029 Stop: 9其原理是即使客户端终止脚本,仍然每隔10秒钟执行一次,并打印出当前时间与终止点,这样就可以测试出ignore_user_abort()函数的具体效果。通过实例发现ignore_user_abort()函数非常实用,实现计划任务,完成后续任务,持续进程等非常有效。更多说明请参与PHP手册。请关注下一期PHP内置函数研究系列。 http://www.cnblogs.com/wgw8299/articles/2170092.html
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具