php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #32665 Premature flushing of output buffer when register_shutdown function called
Submitted: 2005-04-11 08:24 UTC Modified: 2005-04-12 10:08 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: prathap at ee dot pdn dot ac dot lk Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.0.3 OS: Gentoo Linux
Private report: No CVE-ID: None
 [2005-04-11 08:24 UTC] prathap at ee dot pdn dot ac dot lk
Description:
------------
Hello, 

I've posted a miniature version of my code here to explain my doubt. 

<CODE> 

<? 
phpinfo(); 

ob_start(); 

register_shutdown_function("hello"); 

echo ("HELLO"); 

die(); 

function hello() 
{ 

$logfile = "logfile"; 

$error_string = ob_get_contents(); 

if(!$handle = fopen($logfile, "a")) 
{ 
       echo("Could not open logfile for writing!"); 
       ob_end_flush(); 
       exit; 
} 

if(fwrite($handle, $error_string) === FALSE) 
{ 
       echo("Could not write to logfile!"); 
       ob_end_flush(); 
       exit; 
} 

fclose($handle); 

ob_end_clean(); 

} 

?> 

</CODE> 

This simply does not work the way it should. i.e ob_get_contents doen't return 
any string and the buffer just gets flushed by itself at termination. 

Though the PHP manual says it is not possible to retrieve the contents of any 
output buffers using ob_get_contents() with PHP < 4.0.6. But I am using PHP 5. 
x.x 


I posted this problem in the mailing list and I got this response from skrol29:

I've tested your code on PHP 4.3.3 and I have the same behavior has yours. 

"Hello" is output at the end of the PhpInfo data, followed by a PHP notice : 
  Notice: ob_end_clean(): failed to delete buffer. No buffer to delete. in 
... 
And there is an empty "logfile" file created in the Apache directory insteaf 
of the script's directory. 

If you change in your script: 
*********** 
 register_shutdown_function("hello"); 
 echo ("HELLO <br>"); 
 die(); 
*********** 
by 
*********** 
 // register_shutdown_function("hello"); 
 echo ("HELLO <br>"); 
 hello() 
 die(); 
*********** 
then it runs as expected and the "logfile" file is created in the script's 
directory. 

It seems that when calling the shutdown function, all the buffered output is 
immedialty sent to the client and the buffer mode is closed. 
You maybe have PHP bug here, or the manual is wrong about the possibility to 
retrieve the contents. 
But we can notice that this also happens at the end of a script when there 
is no shutdown function.



----

I am still not sure if this is a bug. But I would be very grateful if someone generously volunteers to see to this.

Thank you and congrats on for the good work you have been carrying on all throughout.

Prathap


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-04-11 17:53 UTC] sniper@php.net
It should be said in manual that you can't use ob_* functions in the shutdown funcs since the ob-stuff is already ended before we run these.

 [2005-04-12 09:58 UTC] vrana@php.net
It has been already documented: "There is currently no way to process the data after the request has been completed."

I tried to clarify it to: "There is currently no way to process the data with output buffering functions in the shutdown function."
 [2005-04-12 10:08 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Nov 19 22:00:02 2025 UTC