ob_start() without an ob_flush()

2019-07-15 12:25发布

I hired someone to write an API for me in PHP and MySQL and now have to maintain it myself. I don't know php as well as other languages.

I noticed at the start of most of the php files they have:

ob_start();

I understand that this opens a new output buffer. The thing is that they never flush the buffer. The code had been working fine but I've had a lot of optimization issues, slow server responses, etc.

How is it that they don't have to flush the buffer but the response is still returning?

An example would be:

ob_start();
include "nusoap.php";
include "config.php";
require_once "class.Database.php";
$client = new nusoap_client($config['apiURL'].'/server.php',false, false, false, false, false, 600, 600);

... process the $_GET and build a $result ...
print_r($result);

Obviously the ... process ... is a wide open thing. But I'm not seeing anywhere in there that does any sort of flush or reading the ob contents. I've also searched all the includes and don't see one in there either.

  1. I checked and implicit_flush is set to Off on this server. Since we did move this code from another server possibly on that server it was on. But still currently this API is working on this server.

  2. The reason I said "not that I can find" when someone asked if there was an ob_get_contents() is because there are include files (including nusoap.php) that include other files and while I've grepped through them and tried to trace them, I might have missed something and am still searching. But so far it appears to my eye that no flush or get_contents is happening.

One possible answer is to say "NO, there has to either be an implicit_flush set in the php.ini file, or an implicit_flush() command somewhere, or another flush command somewhere, or getting the contents of the buffer somewhere - or the contents would never output." To me that is what the manual suggests. But sometimes there are loopholes and PHP seems to be a language of loopholes.

SOLVED

Indeed I did a simple test:

<?php
    ob_start();
    echo "Whats Up Doc!";
?>

and the output is seen in the browser.

1条回答
Deceive 欺骗
2楼-- · 2019-07-15 12:35

PHP ob_start() function works by catching all output to buffer and then implicitly output this buffer on script end. You may execute ob_end_clean() to discard (clean) buffer.

In Your example 'print_r($result);' will send output to buffer and then PHP interpreter will send buffer to client (http server/console).

PS. Function ob_implicit_flush() has different meaning. It just flush buffer on every output call (like print or echo), and do not have effect on script finish.

查看更多
登录 后发表回答