2012-07-30 43 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