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);
新聞熱點
疑難解答