# RFC: Continue output buffering

- Status: Implemented (PHP 7.0)
- Authors: Michael Wallner mike@php.net
- Date: 2015-02-19
- Version: 1.1
- Original: https://wiki.php.net/rfc/continue_ob
- Page: https://externals.io/rfc/continue_ob
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Continue output buffering on aborted connection? (closed)

Yes: 15 · No: 0

Voters: aharvey (Yes), davey (Yes), eliw (Yes), frozenfire (Yes), jedibc (Yes), kinncj (Yes), lcobucci (Yes), mbeccati (Yes), ramsey (Yes), rdohms (Yes), rmf (Yes), salathe (Yes), stas (Yes), yohgaki (Yes), yunosh (Yes)

## Discussions on the mailing list

- [\[RFC\]\[DISCUSS\] Continue output buffering despite aborted connection](https://externals.io/message/83179.md) — started on 2015-02-19, 1 messages
- [\[VOTE\] \[RFC\] continue output buffering](https://externals.io/message/84468.md) — started on 2015-03-09, 8 messages

---

## Introduction

The output buffering technique is used to speed up output, but also to capture intermediate output and use it elsewhere.

Often, particularly with functions which directly generate output, this is the only way to accumulate that data.

### Current Status

As of PHP-5.4 and above, which might be a regression I introduced in the rewritten output layer.

#### ignore\_user\_abort = FALSE

The script ends as soon as an aborted connection is detected, either by an unbuffered write or an output exceeding its chunk size (which would result in an unbuffered write).

#### ignore\_user\_abort = TRUE

The script continues to run, but any **already buffered** output will be discarded after processing by any output handler, and more importantly, **any output generated by the script is simply discarded, without ever hitting the output buffer** or any output handler. Output buffers and handlers will just sit in the stack until the output layer is deactivated.

There are a few unimportant functions already broken now:

- phpinfo
- highlight\_{file,string} with return\_output=TRUE
- print\_r, var\_export with return\_output=TRUE
- SoapServer::handle (unimportant because the connection is already broken)

## Proposal

Let the output buffering functions stay usable after the connection was already aborted.

This will be effective only for scripts that do set ignore\_user\_abort, i.e. script execution will end when an aborted connection was detected and ignore\_user\_abort was not set, just as it used to.

#### ignore\_user\_abort = FALSE

Nothing changes.

#### ignore\_user\_abort = TRUE

The output buffer stack **stays intact and usable**. Data generated by a script will be **passed to any output buffers**, which will call respective output handlers to process the buffer.

**Only now** will any generated data of the output handler be **discarded.** That is, before the direct unbuffered write to the SAPI.

### Bacon^WBenefits

Usage of ob\_start(\[callback\]) behaves the same, whether the connection was **aborted or not.** Still, you have to **deliberately enable** it with ignore\_user\_abort(TRUE).

## Backward Incompatible Changes

Output handlers will be called when ignore\_user\_abort is TRUE.

## Proposed PHP Version(s)

PHP 7.0

## Unaffected PHP Functionality

Standard behavior will stay the same.

## Vote

**Vote: Continue output buffering on aborted connection?** — Yes: 15 · No: 0 (closed)

Vote will close on Sunday, March 22nd around 8:00UTC.

## Implementation

```diff
diff --git a/main/output.c b/main/output.c
index 22ac26a..a0a300b 100644
--- a/main/output.c
+++ b/main/output.c
@@ -242,9 +242,6 @@ PHPAPI int php_output_get_status(void)
  * Unbuffered write */
 PHPAPI size_t php_output_write_unbuffered(const char *str, size_t len)
 {
-       if (OG(flags) & PHP_OUTPUT_DISABLED) {
-               return 0;
-       }
        if (OG(flags) & PHP_OUTPUT_ACTIVATED) {
                return sapi_module.ub_write(str, len);
        }
@@ -256,13 +253,13 @@ PHPAPI size_t php_output_write_unbuffered(const char *str, size_t len)
  * Buffered write */
 PHPAPI size_t php_output_write(const char *str, size_t len)
 {
-       if (OG(flags) & PHP_OUTPUT_DISABLED) {
-               return 0;
-       }
        if (OG(flags) & PHP_OUTPUT_ACTIVATED) {
                php_output_op(PHP_OUTPUT_HANDLER_WRITE, str, len);
                return len;
        }
+       if (OG(flags) & PHP_OUTPUT_DISABLED) {
+               return 0;
+       }
        return php_output_direct(str, len);
 }
 /* }}} */
```

## References

A bug about the current behavior: 
<https://bugs.php.net/bug.php?id=67381>

A bug about apparent pre-5.4 behavior: 
<https://bugs.php.net/bug.php?id=64152>

## Changelog

- 1.0 Proposed
- 1.1 Rewordings, list of broken internal functions
