PHP ob_end_flush()没有';t关闭浏览器连接


PHP ob_end_flush() doesn't close browser connection

我们已经依赖它一段时间了,最近更换了服务器。基本上,在php文件结束时,我们会关闭连接,并做一些我们不希望浏览器挂断的繁重工作。根据我的经验,我们使用的这种方法会关闭浏览器连接,用户不会有任何体验。在这个新服务器上,连接保持打开状态,导致浏览器挂起。

我制作了一个非常简单的测试文件:

<?php
ob_end_clean();
ob_start();
?>
<html>
<head>
 <title></title>
</head>
<body>
testing...
</body>
</html>
<?
$size = ob_get_length();
header("Content-Length: $size");
header('Connection: close');
ob_end_flush();
//anything below this should NOT hang up the browser
sleep(30); //but it does :-(
?>

这与PHP无关。您的新服务器可能支持Keep Alive连接(您的浏览器肯定会请求),从而使连接保持打开状态以供后续请求使用。尝试将Header("Connection:close")添加到脚本中,或者关闭web服务器中的Keep Alive支持。