dongyan7876 2012-01-23 20:13
浏览 155
已采纳

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.

Edit: 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.

  • 写回答

3条回答 默认 最新

  • dt614037527 2012-01-23 20:24
    关注

    UPDATE

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

    From the HTTP/1.1 Specification Section 14.10:

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

    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. Buffer ALL the script output so that you retain the capability to send headers
    2. Once you have the full output data, send the appropriate headers to the client
    3. Continue your processing ... but don't try to send output or you'll receive errors because headers have been sent.

    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 ...
    ?>
    

    You might examine the PHP manual section on Connection handlingdocs.

    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();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。