【问题标题】:PHP "header("Connection: close")" and "flush()" not working on production serverPHP“header(”Connection:close“)”和“flush()”在生产服务器上不起作用
【发布时间】:2019-12-09 02:53:04
【问题描述】:

我在 php 中设置了一个使用输出缓冲的新应用程序,代码在本地服务器(wamp)上运行良好,但是当我将代码上传到生产服务器时,代码不起作用,

<?php

ob_end_clean();
header("Connection: close");
ignore_user_abort(true); // just to be safe
ob_start();
echo('Text the user will see ');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush(); // Strange behaviour, will not work
flush(); // Unless both are called !
// Do processing here 
sleep(6);
echo('Text user will never see');

?>

我期待'Text the user will see'的输出,但是服务器并没有关闭连接,但实际输出的是'Text the user will see Text user will never see'

【问题讨论】:

  • 在 PHP 中刷新并不是全部,Web 服务器也必须配合。 // 你实际上想通过这个实现什么?可能有更好的解决方案。
  • @04FS 感谢您的回复,我想在脚本完成所有工作之前向客户端发送响应

标签: php ajax hosting


【解决方案1】:

一些服务器使用 FastCGI 进程管理器。您可能需要考虑将fastcgi finish request 添加到您的代码中。来自文档:

此函数将所有响应数据刷新到客户端并完成请求。这允许在不打开与客户端的连接的情况下执行耗时的任务。

但在不使用 FastCGI 进程管理器时不可用,导致错误,因此您可能需要有条件地添加它:

if ( function_exists("fastcgi_finish_request") ) fastcgi_finish_request();

【讨论】:

    猜你喜欢
    • 2015-09-06
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    • 2020-05-20
    • 2014-06-15
    相关资源
    最近更新 更多