Part of the EllisLab Network
   
1 of 3
1
V1.5 - Output buffer error when loading a view with an error in another view
Posted: 03 November 2006 05:31 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  72
Joined  04-26-2006

I stumbled upon the following possible bug. When I load a view which contains an error in another view (the dirty way :P), then something screws up the output buffering. I have constructed and added a testcase below.

Controller: ViewBug.php

<?php

if(!defined('BASEPATH')) exit('No direct script access allowed');

/******************************************************************************
* ViewBug
******************************************************************************/

class ViewBug extends Controller
{
    
function ViewBug()
    
{
        parent
::Controller();
    
}
    
    
/*
        triggers OB error in Loader.php
    */
    
function test1()
    
{        
        $this
->load->view('viewbug.test1.view.php');
    
}
    
    
/*
        works correct
    */
    
function test2()
    
{
        $viewdata
= array(
                        
            
'view_with_error' => $this->load->view('viewbug.errorview.view.php', null, true)
        );
        
        
$this->load->view('viewbug.test2.view.php', $viewdata);        
    
}
}
?>

View 1: viewbug.test1.view.php

<h1>viewbug.test1.view.php</h1>

<?
echo $this->load->view('viewbug.errorview.view.php', null, true);
// or
// $this->load->view('viewbug.errorview.view.php');
?>

View 2: viewbug.test2.view.php

<h1>viewbug.test2.view.php</h1>

<?
echo $view_with_error;
?>

Error view: viewbug.errorview.view.php

<h1>viewbug.errorview.view.php</h1>
<? a; ?>

Profile
 
 
Posted: 03 November 2006 12:38 PM   [ Ignore ]   [ # 1 ]  
Administrator
Avatar
RankRankRankRankRank
Total Posts:  2505
Joined  12-21-2001

Your code works perfectly for me on PHP 5.1, 4.3, and 4.4.0.  No errors.

Looking at the code I’m not seeing how there can be a buffer error since each view load causes ob_start() to be called.  I wonder if your version of PHP has a bug? 

The error you are getting is just a notice, not a fatal error, so you might just suppress output buffering errors in the Loader.php class.  If you look in that file you’ll find two places where ob_end_clean() is being called.  Change them to @ob_end_clean()

Unless I’m missing some other component in your code somewhere, I’m simply not finding an error.  Sorry…

 Signature 
Profile
MSG
 
 
Posted: 03 November 2006 01:48 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  7
Joined  10-03-2006

I have similar problem. My configuration: PHP 5.1.6 - Xampp for Linux 1.5.4.

Note <? a; ?> in viewbug.errorview.view.php. If variable exists, this problem not occour.

Thanks a lot.

 Signature 

Jessé Jr

Profile
 
 
Posted: 04 November 2006 01:11 PM   [ Ignore ]   [ # 3 ]  
Grad Student
Rank
Total Posts:  72
Joined  04-26-2006

When running test1 i get the following errors:

A PHP Error was encountered.

Severity: Notice

Message
: Use of undefined constant a - assumed 'a'

Filename: _test/viewbug.errorview.view.php

Line Number
: 2

A PHP Error was encountered
.

Severity: Notice

Message
: ob_end_clean() [ref.outcontrol]: failed to delete buffer. No buffer to delete.

Filename: libraries/Loader.php

Line Number
: 648

The first error is correct. The second is not supposed to be there. I know it’s nothing fancy, but there is something wrong with how the loader handles output buffering in combination with PHP errors.

JesseJr - 03 November 2006 01:48 PM

I have similar problem. My configuration: PHP 5.1.6 - Xampp for Linux 1.5.4.

Note <? a; ?> in viewbug.errorview.view.php. If variable exists, this problem not occour.

Thanks a lot.

That “a;” is placed for the purpose of triggering an error.

Profile
 
 
Posted: 04 November 2006 01:19 PM   [ Ignore ]   [ # 4 ]  
Grad Student
Rank
Total Posts:  72
Joined  04-26-2006

If i use this view with test1:

<h1>viewbug.test1.view.php</h1>

<?
$this
->load->view('_test/viewbug.errorview.view.php');
?>

Then there is only the outputbuffer error. There is no way of finding what causes the original error on what line.

So, it’s minor, but worth to mention IMO.

Profile
 
 
Posted: 07 November 2006 06:21 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Rank
Total Posts:  41
Joined  03-29-2006

I have just encountered this bug and in my case it appeared when I tried to call an undefined validator error message, like so:

<?php print $this->validation->description_error;?>

Adding the field to $this->validation->set_fields(); solved the problem.

I don’t think this is expected behaviour.

Profile
 
 
Posted: 07 November 2006 07:33 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Rank
Total Posts:  72
Joined  04-26-2006

Yep, this ‘bug’ can be triggered in several ways.

Profile
 
 
Posted: 07 November 2006 09:03 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  7
Joined  10-03-2006

hlz,

is your compress output option in config\config.php file enabled or disabled?

Jessé Jr

 Signature 

Jessé Jr

Profile
 
 
Posted: 08 November 2006 03:50 AM   [ Ignore ]   [ # 8 ]  
Grad Student
Rank
Total Posts:  72
Joined  04-26-2006
JesseJr - 07 November 2006 09:03 PM

hlz,

is your compress output option in config\config.php file enabled or disabled?

Jessé Jr

It is disabled.

Profile
 
 
Posted: 15 November 2006 06:44 AM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  17
Joined  05-17-2006

This has me total confused.

Here is a view:

<?php
$this
->load->view('page_header',$headers);
$this->load->view('banner');
?>

<div id=content>
<
h1>How am I doing?</h1>
</
div>  <!-- content -->

<?php
$this
->load->view('page_footer');
?>

page_header has an undeclared variable error in it.

If I run it as is then the How Am I Doing appears, but no page_header, banner or page_footer and at the bottom of the page I get

A PHP Error was encountered

Severity
: Notice

Message
: ob_end_clean() [ref.outcontrol]: failed to delete buffer. No buffer to delete.

Filename: libraries/Loader.php

Line Number
: 648

If I look at the debug log I see the original error and each of the views being loaded

ERROR - 2006-11-15 11:36:12 --> Severity: Notice Notice --> Undefined variable: js /Users/phoebebr/Sites/ci151/system/application/views/page_header.php 28
ERROR
- 2006-11-15 11:36:12 --> Severity: Notice Notice --> Undefined variable: js /Users/phoebebr/Sites/ci151/system/application/views/page_header.php 29
DEBUG
- 2006-11-15 11:36:12 --> File loaded: /Users/phoebebr/Sites/ci151/system/application/views/page_header.php
DEBUG
- 2006-11-15 11:36:12 --> File loaded: /Users/phoebebr/Sites/ci151/system/application/views/banner.php
DEBUG
- 2006-11-15 11:36:12 --> File loaded: /Users/phoebebr/Sites/ci151/system/application/views/page_footer.php
DEBUG
- 2006-11-15 11:36:12 --> File loaded: /Users/phoebebr/Sites/ci151/system/application/views/home.php
ERROR
- 2006-11-15 11:36:12 --> Severity: Notice Notice --> ob_end_clean() [<a href='ref.outcontrol'>ref.outcontrol</a>]: failed to delete buffer. No buffer to delete. /Users/phoebebr/Sites/ci151/system/libraries/Loader.php 648
DEBUG
- 2006-11-15 11:36:12 --> Final output sent to browser
DEBUG
- 2006-11-15 11:36:12 --> Total execution time: 0.0710

Here is where it gets strange.  If I put an echo statement in Loader just above the logging code and comment out the buffers like this:

//    ob_start();
                
        // If the PHP installation does not support short tags we'll
        // do a little string replacement, changing the short tags
        // to standard PHP echo statements.
        
if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
        
{
            
echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($path))).'<?php ');
        
}
        
else
        
{
            
include($path);
        
}
        
        
echo '['.$path.']';
        
log_message('debug', 'File loaded: '.$path);
        
        
// Return the file data if requested
        
if ($return === TRUE)
        
{        
    
//        $buffer = ob_get_contents();
//            ob_end_clean();
            
            
return $buffer;
        
}

I see the banner and page_footer views being loaded BUT NOT page_header

However, if I alter my view to call page_header twice

<?php
$this
->load->view('page_header',$headers);
$this->load->view('page_header',$headers);
     
$this->load->view('banner');

?>


I see the page_header view being loaded and the errors are correctly reported.

page_header
A PHP Error was encountered

Severity
: Notice

Message
: Undefined variable: js

Filename
: views/page_header.php

Line Number
: 28

A PHP Error was encountered

Severity
: Notice

Message
: Undefined variable: js

Filename
: views/page_header.php

Line Number
: 29

[
/Users/phoebebr/Sites/ci151/system/application/views/page_header.php]


This is getting too deep for me!!!!

Profile
 
 
Posted: 15 November 2006 01:30 PM   [ Ignore ]   [ # 10 ]  
Research Assistant
RankRankRank
Total Posts:  970
Joined  04-13-2006
Phoebe - 15 November 2006 06:44 AM

This is getting too deep for me!!!!

Phoebe, you’re not alone! There have been a number of “php errors not being reported” posts, including my own.
In my own experience, I could spot my error pretty quickly and it was usually one or more undefined variables in a view.
Whether this is a CI or a PHP bug, I have no idea. But there does appear to be a connection between “bad” CI views and the failure to display any php error message.

Profile
 
 
Posted: 15 November 2006 06:20 PM   [ Ignore ]   [ # 11 ]  
Grad Student
Avatar
Rank
Total Posts:  56
Joined  05-10-2006

Mark me down as experiencing this as well. I am having this occur on my development server which uses PHP 5.1.6 & Apache/1.3.37. I am using CI 1.5.0.1.

What is weird is that sometimes it is totally blank, and sometimes I get the output buffer error described in the first post.

Profile
 
 
Posted: 16 November 2006 06:46 AM   [ Ignore ]   [ # 12 ]  
Research Assistant
RankRankRank
Total Posts:  970
Joined  04-13-2006

Commenting out the ob_end_clean() call towards the end of show_php_error() in Exceptions.php seems to fix this for me.
Obviously this may create new problems elsewhere, though. I’m only looking at one particular scenario at the moment: bad or missing variable in a view file. Output buffering gurus required from here on….
UPDATE: On the other hand, ob_end_clean() was in 1.4.1 with no problem (for me, anyway). Which brings me back to the lines which were new to show_php_error() in 1.5 :

if (ob_get_level() > 1)
        
{
            ob_end_flush
();    
        
}


Rick has previously told me that these lines were added to fix another problem, although I can’t remember what it was.
Commenting out the ob_end_flush() call fixes my problem; also I found this in php help which looks significant:

User contributed notes:
(18-May-2005 04:45)

Sometimes, ob_get_level() may be off by 1 because at the start of the script, it will return 1 even if ob_start() has never been called (and clearing the output buffer via ob_end_clean() and the like can be done without error).  As a result, the first ob_start() will have an ob_get_level() of 2, the second will be 3, and so on.
I’m not sure if this is a PHP 5 thing or possibly because our server is set to gzip all html documents.

I’m developing on php 5.0.4.
I think the ob_get_level() call needs a re-think. Better people than me, apply now.

Profile
 
 
Posted: 19 November 2006 02:33 PM   [ Ignore ]   [ # 13 ]  
Administrator
Avatar
RankRankRankRankRank
Total Posts:  2505
Joined  12-21-2001

I think I have a solution, but since I’m unable to duplicate the problem locally I would appreciate it very much if you guys can test it for me.  Here’s the proposed fix:


Open:  libraries/Exceptions.php

In the constructor, add this:

$this->ob_level = ob_get_level();

Your constructor will now look like this:

function CI_Exceptions()
{
    $this
->ob_level = ob_get_level();
}


Next, at lines 120 AND 156 change this:

if (ob_get_level() > 1)
{
    ob_end_flush
();    
}

To this:

if (ob_get_level() > $this->ob_level + 1)
{
    ob_end_flush
();    
}

 Signature 
Profile
MSG
 
 
Posted: 19 November 2006 03:08 PM   [ Ignore ]   [ # 14 ]  
Summer Student
Total Posts:  17
Joined  05-17-2006

Yes!

That fixed it for me.

kiss

Profile
 
 
Posted: 19 November 2006 03:39 PM   [ Ignore ]   [ # 15 ]  
Research Assistant
RankRankRank
Total Posts:  970
Joined  04-13-2006

Looks good to me, too. (Ellismania suddenly erupts! Cue singing, dancing, drinking, er, more drinking…)

Profile
 
 
   
1 of 3
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 66430 Total Logged-in Users: 28
Total Topics: 84793 Total Anonymous Users: 5
Total Replies: 455043 Total Guests: 202
Total Posts: 539836    
Members ( View Memberlist )
Newest Members:  Dylan1978X_franbaguasllogocsaturkeyPeter BryanttherendStudioGeorgiaJZeerfedeghe