PHP:关闭输出流 [英] PHP: close output stream

查看:135
本文介绍了PHP:关闭输出流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以关闭PHP脚本的输出流?我有一个脚本需要做一些后期处理,但在后期处理期间和之后它不再向客户端发送任何数据,所以我想在后期处理之前关闭连接。

Is it possible to close the output stream of a PHP script? I have a script which needs to do some post processing, but during and after the post processing it won't send any data to the client anymore, so I'd like to close the connection before the post processing.

编辑:在我的应用程序中,我有一个需要不时重建的缓存。但是,我不想让用户放慢速度。我想要的是在脚本的最后确定是否需要重建缓存。所以我想首先关闭输出流,以便用户获取它的数据,然后我想重建缓存。这样做并不是非常关键,但我认为最好先关闭连接,这样用户就不会注意到缓存正在重建,如果需要很长时间。

In my application I have a cache which needs to be rebuild every now and then. However, I don't want to slow a user down. What I want is to determine at the end of the script if the cache needs to be rebuild. So I want to close the output stream first, so the user gets it's data, and then I want to rebuild the cache. It isn't really critical to do this, but I think it's better to close the connection first, so the user won't notice that the cache is being rebuild if that takes a long time.

推荐答案

UPDATE

处理这种情况的方法是输出的组合缓冲和相应的HTTP标头。

The way to handle this case is a combination of output buffering and the appropriate HTTP headers.

来自 HTTP / 1.1规范第14.10节


HTTP / 1.1定义关闭发件人的连接选项为
表示在完成
回复后连接将被关闭。

HTTP/1.1 defines the "close" connection option for the sender to signal that the connection will be closed after completion of the response.

因此,如果除了 Connection:close 之外我们传递HTTP Content-Length 标头,浏览器知道在收到指定的响应长度后关闭连接:

So, if we pass along an HTTP Content-Length header in addition to Connection: close, the browser knows to close the connection after the specified response length is received:


  1. 缓冲所有脚本输出以便保留发送标题的功能

  2. 获得完整输出数据后,将相应的标题发送到客户端

  3. 继续处理...但不要尝试发送输出,否则你会收到错误,因为标题已经发送。

此外,是小心,因为如果你做了太多的处理,你可以在Web服务器SAPI中遇到脚本执行时间限制。最后,您应该告诉PHP使用 ignore_user_abort() 因为浏览器将因您正在执行的操作而关闭连接,并且您希望PHP继续处理。

Also, be careful as you can run up against script execution time limits in web server SAPI if you do too much processing. Finally, you should tell PHP to ignore a "user abort" in this particular script using ignore_user_abort() because the browser will close the connection as a result of what you're doing and you want PHP to continue processing.

<?php
ignore_user_abort();
ob_start();

// do stuff, generate output

// get size of the content
$length = ob_get_length();

// tell client to close the connection after $length bytes received
header('Connection: close');
header("Content-Length: $length");

// flush all output
ob_end_flush();
ob_flush();
flush();

// close session if you have one ...
// continue your processing tasks ...
?>






您可以查看关于 连接处理 docs

或者,为什么不开始输出缓冲?然后你可以捕获所有将要发送的输出然后决定你是否真的想用它做任何事情。

Alternatively, why not start output buffering? Then you can capture all the output that would be sent then decide later if you actually want to do anything with it.

<?php

echo 'before output buffering';
ob_start();
echo 'after output buffering';
$output = ob_get_contents();

// script's only output to this point will be 'before output buffering'

// I changed my mind, send the output ...
ob_end_flush();
?>

这篇关于PHP:关闭输出流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆