【问题标题】:PHP drop HTTP connection [duplicate]PHP丢弃HTTP连接[重复]
【发布时间】:2012-07-28 04:54:34
【问题描述】:

可能重复:
close a connection early
How to continue process after responding to ajax request in PHP?

我需要在 ajax 操作中断开连接:

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

我该怎么做?

【问题讨论】:

标签: php


【解决方案1】:

只需让用户断开连接,但让脚本继续运行。

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

【讨论】:

  • 我想我应该在连接之前计算内容长度:关闭?
  • PHP 应该自动完成。
  • BTW haders 应该在任何输出之前。
  • 我测试了代码:如果没有 content-length 标头,它在我的 linux 服务器上不起作用。
猜你喜欢
  • 2016-02-09
  • 1970-01-01
  • 2019-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多