ignore_user_abort

(PHP 4, PHP 5, PHP 7, PHP 8)

ignore_user_abort設(shè)置客戶端斷開連接時是否中斷腳本的執(zhí)行

說明

ignore_user_abort(bool $value = ?): int

設(shè)置客戶端斷開連接時是否中斷腳本的執(zhí)行

PHP 以命令行腳本執(zhí)行時,當(dāng)腳本終端結(jié)束,腳本不會被立即中止,除非設(shè)置 valuetrue,否則腳本輸出任意字符時會被中止。

參數(shù)

value

如果設(shè)置了該值,函數(shù)會把 ignore_user_abort ini 的值設(shè)置為 value。 如果未設(shè)置該值,函數(shù)不會改變設(shè)置,僅會返回之前的設(shè)置。

返回值

以整型返回之前的設(shè)置

范例

示例 #1 ignore_user_abort()例子

<?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嘗試發(fā)送信息到客戶端之前,不會檢測到用戶是否已中斷連接。 僅使用 echo 語句不能確保信息已發(fā)送,參見 flush() 函數(shù)。

參見