|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
|
Thank you for your help!
If the status of the bug report you submitted changes, you will be notified.
You may return here and check the status or update your report at any time. The URL for your bug report is: https://bugs.php.net/bug.php?id=42334.
[2007-08-18 01:49 UTC] ahaig at penguinmililtia dot net
Description:
------------
An error that occurs after ob_start() has been called causes the buffer to flush.
This makes it impossible to appropriately manage output for error handling because sometimes text will randomly be inserted prior to the error output (which means the error output has a full <html></html> tag set, but content gets output before it - often open tags - which messes up tag pairing and display).
I ran into this bug using Smarty templating. Smarty uses ob_get_contents() to grab output from an include() of a compiled template that takes place inbetween ob_start() and ob_end_clean(). Error handling for calls inside the compiled template (which ends up being just combined php and html with multiple <?php ?> pairs) end up with junk before their proper output.
Reproduce code:
---------------
<?php
ob_start();
echo 'test';
trigger_error('error', E_USER_ERROR);
$output = ob_get_contents();
ob_end_clean();
?>
Expected result:
----------------
I expect to see output from trigger_error() (or any other error php might throw) and nothing else. Non-error output should be buffered and not displayed unless requested. Error output constitutes an exception to normal processing, so should be output.
The only other logical functioning I can see (although this would not, in my mind, be the preferred functioning) is that the code should output nothing at all, even the error output should go to the output buffer. This would be absolutely consistent for the functions, but would make it impossible to do any error handling inside a buffered section.
Actual result:
--------------
Echo outputs 'test' and trigger_error outputs an error.
PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 04:00:02 2025 UTC |
Output buffer is flushed when PHP terminates execution. Current PHP cannot catch E_ERROR. This prevents cleaning up buffer, but not E_USER_ERROR. e.g. set_error_handler(function ($errno, $errstr, $errfile, $errline) { if (!(error_reporting() & $errno)) { // This error code is not included in error_reporting return; } switch ($errno) { case E_USER_ERROR: ob_clean(); //////////////// CLEAN UP BUFFER ////////////////////// echo "<b>My ERROR</b> [$errno] $errstr<br />\n"; echo " Fatal error on line $errline in file $errfile"; echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n"; echo "Aborting...<br />\n"; exit(1); break; case E_USER_WARNING: echo "<b>My WARNING</b> [$errno] $errstr<br />\n"; break; case E_USER_NOTICE: echo "<b>My NOTICE</b> [$errno] $errstr<br />\n"; break; default: echo "Unknown error type: [$errno] $errstr<br />\n"; break; } /* Don't execute PHP internal error handler */ return true; }); ob_start(); echo 'test'; trigger_error('error', E_USER_ERROR); $output = ob_get_contents(); ob_end_clean(); ?> We have not many problematic E_ERRORs in our code base now. Most problematic E_ERRORs are in SOAP module.One possible way to create a fatal error (ok, only if programmer is lazy) function foo() ... if (<usually false>) foop(); // spelling error As long as the if-clause is false the error won't be noticed.