Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7085475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:25:44+00:00 2026-05-28T07:25:44+00:00

Is it possible to close the output stream of a PHP script? I have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:25:45+00:00Added an answer on May 28, 2026 at 7:25 am

    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();
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to close parent window in Firefox 2.0 using JavaScript. I have
Is it possible somehow to close StreamReader after calling ReadToEnd method in construction like
Is it possible to get list after Session.Close as below? var bundles = session.Linq<Bundle>();
is it possible to use something like an vim-close/exit-Event to execute some last commands
I have a very large object which I wish to serialize. During the process
I have a GUI application, which creates a QProcess inside, catches its output and
I have noticed that cURL in PHP returns different data when told to output
I am not sure if this is possible...i have code within my application which
Is it possible to close an InfoPath form programmatically? I know that it can
Possible Duplicate: Cross-thread operation not valid I am trying to close the base of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.