PHP: close output stream
Asked Answered
U

3

7

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.

Uptotheminute answered 23/1, 2012 at 20:13 Comment(2)
Why do you need to do this? Can you post a bit of your code to help us understand a bit more?Chemotaxis
+1 to what Jonathan said. Tell us a bit more about why you want to do this.Impetrate
C
8

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();
?>
Cruelty answered 23/1, 2012 at 20:24 Comment(5)
Output buffering still keeps the output stream to the browser open. So suppose I need to do some post processing which takes a few seconds or so, the browser will wait until that post processing is done. Especially with AJAX this will slow down the webapplication a lot. So I want to be able to close the connection to the browser, so the browser can process the output, and then continue with the post processing.Uptotheminute
@Uptotheminute I've updated my answer with (what I believe is) the correct solution.Cruelty
That's a pretty nice solution! I'm going to try that in a minute. Edit: It works great! Thanks a lot!Uptotheminute
@Uptotheminute one more thing -- I added that you should call ignore_user_abort() at the top of your script. This may have been evident from the PHP manual link in my original answer, but I wanted to state it explicitly. So, just make sure you do that :)Cruelty
Ok thank you very much! In my script I've also added output buffering for everything after the original output is send, so if any output is accidentally send I can just discard it and the script won't crash.Uptotheminute
P
0

fclose(STDOUT): http://php.net/manual/en/features.commandline.io-streams.php

Palp answered 23/1, 2012 at 20:15 Comment(0)
B
0

I have not enough reputation to comment, but I want to share that in the @rdlowrey answer gzip could be a problem.

If you have gzip enabled the Transfer-encoding header is always set to chunked, even if you try to change it with header("Transfer-encoding: none"); therefore it won't send the Content-Length header.

The way I could solve this was using the next before:

<?
@ini_set('zlib.output_compression', 'Off');
@ini_set('output_buffering', 'Off');
@ini_set('output_handler', '');
@apache_setenv('no-gzip', 1);
?>

And then the solution:

<?
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();
flush();

// close session if you have one ...
// continue your processing tasks ...
?>
Buerger answered 3/6, 2015 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.