It seems that while using output buffering, an included file which calls die() before the output buffer is closed is flushed rather than cleaned. That is, ob_end_flush() is called by default.
<?php
// a.php (this file should never display anything)
ob_start();
include('b.php');
ob_end_clean();
?>
<?php
// b.php
print "b";
die();
?>
This ends up printing "b" rather than nothing as ob_end_flush() is called instead of ob_end_clean(). That is, die() flushes the buffer rather than cleans it. This took me a while to determine what was causing the flush, so I thought I'd share.
CXIV. Funkce pro řízení výstupu
Úvod
Funkce pro řízení výstupu vám umožňují ovládat, kdy se odešle výstup skriptu. To může být užitečné v několika různých situacích, zvláště pokud potřebujete poslat browseru hlavičky poté, co váš skript začal odesílat data. Output Control funkce neovlivňují hlavičky odeslané pomocí funkcí header() nebo setcookie(), pouze funkce jako echo() a data mezi bloky PHP kódu.
Požadavky
Tyto funkce jsou k dispozici jako součást standardního modulu, který je vždy dostupný.
Instalace
K používání těchto funkcí není třeba žádná instalace, jsou součástí jádra PHP.
Konfigurace běhu
Chování těchto funkcí je ovlivněno nastavením parametrů v php.ini.
Tabulka 211. Konfigurační volby řízení výstupu
| Název | Výchozí hodnota | Lze změnit |
|---|---|---|
| output_buffering | "0" | PHP_INI_PERDIR|PHP_INI_SYSTEM |
| output_handler | NULL | PHP_INI_PERDIR|PHP_INI_SYSTEM |
| implicit_flush | "0" | PHP_INI_PERDIR|PHP_INI_SYSTEM |
Pro bližší podrobnosti a definici PHP_INI_* konstant viz ini_set().
Zde je stručný popis konfiguračních direktiv.
- output_buffering boolean/integer
Řízení výstupu můžete povolit pro všechny soubory nastavením této direktivy na 'On'. Pokud chcete omezit velikost bufferu na určitou hodnotu, můžete jako hodnotu této direktivy místo 'On' použít maximální počet bytů (např. output_buffering=4096).
- output_handler string
Celý výstup vašich skriptů můžete přesměrovat do funkce. Například pokud nastavíte output_handler na mb_output_handler(), kódování znaků bude transparentně překódováno do zadaného kódování. Nastavení jakékoliv funkce automaticky zapne řízení výstupu.
Poznámka: Nemůžete používat najednou mb_output_handler() s ob_inconv_handler() a nemůžete používat najednou ob_gzhandler() a zlib.output_compression.
- implicit_flush boolean
Ve výchozí nastavení je FALSE. Změna na TRUE oznámí PHP, aby oznámilo výstupní vrstvě, aby se automaticky vyprázdnila po každém vypsaném bloku. Je to ekvivalentní volání PHP funkce flush() po každém zavolání funkce print() nebo echo() a po každém HTML bloku.
Když se PHP používá v prostředí webu, zapnutí této volby má závažné výkonnostní důsledky a obecně se doporučuje ho používat pouze pro ladění. Tato hodnota má výchozí nastavení TRUE pokud pracujete pod CLI SAPI.
Viz také ob_implicit_flush().
Typy prostředků
Toto rozšíření nemá definován žádný typ prostředku (resource).
Předdefinované konstanty
Toto rozšíření nemá definovány žádné konstanty.
Příklady
Příklad 1410. Ukázka řízení výstupu
<?php
ob_start();
echo "Ahoj\n";
setcookie ("cookiename", "cookiedata");
ob_end_flush();
?>
Ve výše uvedené ukázce se výstup z echo() uloží ve výstupním bufferu až do volání ob_end_flush(). Mezitím volání setcookie() úspěšně uložilo cookie bez vyvolání chyby. (Normálně nemůžete odeslat do browseru hlavičky poté, co už byla odeslána data.)
Poznámka: Při přechodu z PHP 4.1 (a 4.2) na 4.3 musíte kvůli chybě ve starších verzích zaručit, aby v php.ini byla direktiva implict_flush nastavena na OFF, jinak výstup po zavolání funkce ob_start() nebude skryt z výstupu.
Viz také
Viz také header() a setcookie().
Obsah
- flush — Odeslat výstupní buffer
- ob_clean — Clean (erase) the output buffer
- ob_end_clean — Vyčistit (vymazat) výstupní buffer a vypnout bufferování výstupu
- ob_end_flush — Vyprázdnit (odeslat) výstupní buffer a vypnout bufferování výstupu
- ob_flush — Flush (send) the output buffer
- ob_get_clean — Get current buffer contents and delete current output buffer
- ob_get_contents — Vrátit obsah výstupního bufferu
- ob_get_flush — Flush the output buffer, return it as a string and turn off output buffering
- ob_get_length — Vrátit délku výstupního buffer
- ob_get_level — Return the nesting level of the output buffering mechanism
- ob_get_status — Get status of output buffers
- ob_gzhandler — ob_start callback function to gzip output buffer
- ob_implicit_flush — Vypnout/zapnout implicitní flush
- ob_list_handlers — List all output handlers in use
- ob_start — Zapnout bufferování výstupu
- output_add_rewrite_var — Add URL rewriter values
- output_reset_rewrite_vars — Reset URL rewriter values
Funkce pro řízení výstupu
29-Jun-2007 07:02
28-Jan-2007 12:45
Please note that most browsers don't display a table unless they passed its end-tag "</table>".
Thats why using this feature in HTML-tables might not result in what you expected...
best regards
BasicArtsStudios
21-Jan-2007 10:39
Sometimes you might not want to include a php-file under the specifications defined in the functions include() or require(), but you might want to have in return the string that the script in the file "echoes".
Include() and require() both directly put out the evaluated code.
For avoiding this, try output-buffering:
<?php
ob_start();
eval(file_get_contents($file));
$result = ob_get_contents();
ob_end_clean();
?>
or
<?php
ob_start();
include($file);
$result = ob_get_contents();
ob_end_clean();
?>
which i consider the same, correct me if I'm wrong.
Best regards, BasicArtsStudios
16-Sep-2006 02:08
Unfortunately, the PHP guys didn't build support into any of the image output functions to return the image instead of outputting it.
Fortunately, we have output buffering to fix that.
<?
$im = imagecreatetruecolor(200, 200);
// Other image functions here...
ob_start();
imagepng($im);
$imageData = ob_get_contents();
ob_clean();
?>
You can now use the $imageData variable to either create another GD image, save it, put it in a database, make modifications to the binary, or output it to the user. You can easily check the size of it as well without having to access the disk...just use strlen();
07-Sep-2006 04:07
Now this just blew my mind. I had a problem with MySQL being incredibly slow on Windows 2003 running IIS... on ASP/VBScript pages. PHP is also installed on the server and so is Microsoft SQL 2005 Express. (Yes, we're running ASP, PHP, MySQL and MS SQL on the same Windows 2003 Server using IIS.)
I was browsing the internet for a solution and saw a suggestion that I change output_buffering to on if MySQL was slow for PHP pages. Since we also served PHP pages with MySQL from the same server, it caught my eye. For the hell of it, I went into php.ini and changed output_buffering to on and suddenly MySQL and ASP was faster... MySQL and PHP was faster... Microsoft SQL Server 2005 Express and ASP was faster.... everything was faster... even stuff that had no PHP!
And I didn't even have to restart IIS. As soon as I saved the php.ini file with the change, everything got faster.
Apparently PHP and MySQL and IIS are so intertwined somehow that changing the buffering setting really effects the performance of the entire server.
So, if you are having performance problems on Windows 2003 & IIS, you might try setting output_buffering = On in php.ini if you happen to have PHP installed. Having it set to off apparently effects the performance of Windows 2003 and IIS severely... even for webpages that do not use PHP or MySQL.
21-Aug-2006 01:30
Output buffering is set to '4096' instead of 'Off' or '0' by default in the php-5.0.4-10.5 RPM for Fedora Core release 4 (Stentz). This has cost me much time!
10-Jul-2006 06:00
In re to erwinX at darwineX dot nl:
Adding an ampersand (&) before the hash seems to work too (for me at least), i.e.: http://somedomain.tld/blah.php?arg=x&#something
I guess php then interperts it as an argument to the script. Might save some time and resources.
11-Nov-2005 05:35
[Concerns IE refusing to jump to a #something in the URL.]
I encoutered a bug in IE6/W2000 that can be solved by turning output buffering on.
Maybe it also helps in other situations/M$-OS, not sure.
Situation:
A page with a hash in the URL, and IE doesn't jump to that location.
Example:
http://www.bla.com/test.php#something
- In test.php the anchortag is placed normally like:
<a name="something"><br></a>
- test.php takes a few seconds to load because of heavy-duty database activity.
IE just ignores the hash #something.
It looks like IE 'forgets' the hash if it hasn't encoutered it YET in the HTML.
Turning output buffering on resolves that issue.
23-Jun-2005 11:25
I ran out of memory, while output buffering and drawing text on imported images. Only the top portion of the 5MP image was displayed by the browser. Try increasing the memory limit in either the php.ini file( memory_limit = 16M; ) or in the .htaccess file( php_value memory_limit "16M" ). Also see function memory_get_usage() .
10-Jul-2004 05:53
For those who are looking for optimization, try using buffered output.
I noticed that an output function call (i.e echo()) is somehow time expensive. When using buffered output, only one output function call is made and it seems to be much faster.
Try this :
<?php
your_benchmark_start_function();
for ($i = 0; $i < 5000; $i++)
echo str_repeat ("your string blablabla bla bla", (rand() % 4) + 1)."<br>\n";
echo your_benchmark_end_function();
?>
And then :
<?php
your_benchmark_start_function();
ob_start ();
for ($i = 0; $i < 5000; $i++)
echo str_repeat ("your string blablabla bla bla", (rand() % 4) + 1)."<br>\n";
echo your_benchmark_end_function();
ob_end_flush ();
?>
01-Apr-2004 04:49
There is a problem in MSIE 5.5,6 with regards to Page compression. Users might experience pages not loading completely, or just a blank page.
This articles you are looking for is what you're looking for:
Microsoft Knowledge Base Article - 312496 (for MSIE 6)
Microsoft Knowledge Base Article - 313712 (for MSIE 5.5)
It states that you should upgrade to the latest MSIE Service Pack to fix the following problem:
Internet Explorer May Lose the First 2,048 Bytes of Data That Are Sent Back from a Web Server That Uses HTTP Compression
09-Jul-2003 04:44
Trying to benchmark your server when using output_buffering ?
Don't forget that the value 4096 in the php.ini will give you complete different loadtimes compares to the value of 1.
In the first case the output will be sent after buffering 4096 and the loadtime timed at the end of the page will contain the loadtime needed to download the complete page in the clientbrowser while the second value will contain the loadtime needed to place the complete page in the buffer. The time needed for sending is not clocked.
This can be very frustrating if you don't see the differance between server and the 1st is using 4096 instead of 1.
Although technically much faster than the second server the second server was providing much better loadtime results.
This result will grow when using large amounts of output.
But this becomes interesting if you want to measure the time needed for the page to be loaded for the client.
08-Feb-2001 11:17
A few tutorials exist on this subject :
* http://www.zend.com/zend/art/buffering.php
* http://www.phpbuilder.com/columns/argerich20010125.php3
