2012-07-30 29 views
1

可能重複:
close a connection early
How to continue process after responding to ajax request in PHP?PHP下降HTTP連接

我需要在阿賈克斯行動,以點連接:

$response = getResponse($params); 
echo $response; // don't make user to wait 
flushAndDropConnection(); 
someLongActionsFor5Seconds(); 

我該怎麼辦呢?

+3

可能重複[如何在PHP中響應ajax請求後繼續進程?](http://stackoverflow.com/questions/1481247/how-to-continue-process-after-responding-to- ajax-request-in-php)或http://stackoverflow.com/questions/4806637/continue-processing-after-closing-connection或http://stackoverflow.com/questions/138374/close-a-connection-early或 – ManseUK 2012-07-30 13:52:44

+0

您應該閱讀PHP文檔中關於'flush()'的討論。這篇文章具體:http://www.php.net/manual/en/function.flush.php#91556 – Tchoupi 2012-07-30 13:54:24

+0

重複的解決方案不起作用。 – DmitryR 2012-07-30 14:08:22

回答

2

只是讓用戶斷開連接,但讓腳本繼續運行。

ignore_user_abort(true); 
ob_start(); 
echo "response"; 
$size = ob_get_length(); 
header("Content-Length: $size"); 
header('Connection: close'); 
ob_end_flush(); 
ob_flush(); 
flush(); 
if (session_id()) session_write_close(); 
//your long running action here 
+0

我想我應該在連接前計算內容長度:關閉? – DmitryR 2012-07-30 13:56:54

+0

PHP應該自動執行該操作。 – iblue 2012-07-30 13:57:46

+0

BTW哈德應該在任何輸出之前。 – DmitryR 2012-07-30 14:08:58