Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: close output stream

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.

like image 391
Tiddo Avatar asked Jan 23 '12 20:01

Tiddo


1 Answers

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();
?>
like image 105
rdlowrey Avatar answered Oct 19 '22 17:10

rdlowrey