Home > php教程 > php手册 > body text

PHP 函数 ignore

WBOY
Release: 2016-06-06 19:54:42
Original
1405 people have browsed it

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,会导致脚本停止运行。
如果未设置该参数,会返回当前的设置。

 注释: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.

  ?>

  

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,函数基础用法

?

1

2

3

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

?>

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


3,自定义实现文件输出并跟踪ignore_user_abort()函数的执行结果

?

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

?>

打开liuhui.php文件,文件内容如下:

Current Time: 1273735029 Stop: 9

其原理是即使客户端终止脚本,仍然每隔10秒钟执行一次,并打印出当前时间与终止点,这样就可以测试出ignore_user_abort()函数的具体效果。

通过实例发现ignore_user_abort()函数非常实用,实现计划任务,完成后续任务,持续进程等非常有效。更多说明请参与PHP手册。请关注下一期PHP内置函数研究系列。

 

http://www.cnblogs.com/wgw8299/articles/2170092.html

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!