2011-06-17 89 views
2

我有以下代碼:PHP connection_aborted()工作不正常

ignore_user_abort(true); 
while(!connection_aborted()) { 
    // do stuff 
} 

,並根據PHP文檔,這應該運行,直到連接被關閉,但由於某些原因,它沒有,而是它會一直運行,直到腳本超時。我在網上看了一下,有人建議加入

echo chr(0); 
flush(); 

進入循環,但這似乎也沒有做任何事情。更糟糕的是,如果我只是將其保留爲

while(true) { 
    // do stuff 
} 

在客戶端斷開連接後,PHP仍然繼續運行腳本。有誰知道如何得到這個工作?有沒有一個php.ini設置,我錯過了某個地方?

如果有問題,我正在運行PHP 5.3.5。提前致謝!

回答

1

嘗試:

ignore_user_abort(true); 

    echo "Testing connection handling"; 

    while (1) { 
      if (connection_status() != CONNECTION_NORMAL) 
        break; 
      sleep(1); 
      echo "test"; 
      flush(); 
    } 
+0

不,也沒有出現工作。值得注意的是,當我在瀏覽器中運行它時,我沒有看到每秒出現「測試」。換句話說,它看起來不像腳本沖洗,導致我相信它可能是我失蹤的php.ini設置? – kevmo314 2011-06-17 04:49:40

+0

什麼是在你的php.ini中設置的'output_buffering'設置? – 2011-06-17 04:52:20

+1

output_buffering = 4096 我應該嘗試將其設置爲「關」嗎? – kevmo314 2011-06-17 04:53:58

1

儘量只flush();之前使用ob_flush();和一些瀏覽器中加入了一些數據之前是不會更新頁面。

嘗試做這樣的事情

<? php 
// preceding scripts 

ignore_user_abort(true); 

$i = 0; 

while(!connection_aborted()) 
{ $i++; 
    echo $i; 

    echo str_pad('',4096); // yes i know this will increase the overhead but that can be reduced afterwords 

    ob_flush(); 

    flush(); 

    usleep(30000); // see what happens when u run this on my WAMP this runs perfectly 
} 

// Ending scripts 
?> 

谷歌瀏覽器有這個代碼,實際的問題;它不能很好地支持流式傳輸。

0

不幸的是,我有output_buffering =關閉,無論如何,我的while循環繼續運行,connection_aborted()給出0即使客戶端關閉瀏覽器。 任何其他提示?