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()函数 和一个死循环就可以实现计划任务功能.
<?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 . "\n" ; if (fwrite( $handle , $time . "\n" )===false) { echo "not write file:" . $file ; exit ; } echo "write file time:" . $time . "\n" ; $i ++; sleep(2); } fclose( $handle );加上这段代码,即使你把浏览器关闭后还是能还执行php计划任务哦.
查看更多关于php ignore_user_abort()函数之计划任务实现方法 - ph的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did30609