Advertisement

ob_flush pushes output before Joomla header

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
chasiv
Joomla! Intern
Joomla! Intern
Posts: 59
Joined: Sat Feb 03, 2007 3:02 am
Location: Peru, ME USA
Contact:

ob_flush pushes output before Joomla header

Post by chasiv » Mon Sep 30, 2019 6:39 pm

I have a module that I've created to test ob_flush buffering off. I need to understand it so I can have some longer modules buffer output to the page as the script goes through long processes. The code is working, however it is showing above the header of the page instead of in the module section (between the header and footer). I understand the code, I simply do not understand why Joomla is processing this outside the proper module window.

Here is the output buffering code:

Code: Select all

    <?php
    // Turn off output buffering
    ini_set('output_buffering', 'off');
    // Turn off PHP output compression
    ini_set('zlib.output_compression', false);
             
    //Flush (send) the output buffer and turn off output buffering
    while (@ob_end_flush());
             
    // Implicitly flush the buffer(s)
    ini_set('implicit_flush', true);
    ob_implicit_flush(true);
    ob_start();
     
    for($x=0; $x<25;$x++) {
    	echo "X=" . $x;	
    	echo str_pad("",1024," ");
    	echo "<br />";
    	ob_flush();
    	flush();
    	 
    	sleep(1);        
    } 
     
     
    echo "Program Output";
    ob_flush();
    flush();
    ?>
Any insight on how to get this working properly would be appreciated. Here is a picture of the output:

Image

Thanks,
Chas
Last edited by toivo on Tue Oct 01, 2019 12:17 am, edited 1 time in total.
Reason: mod note: moved from 3.x General Questions

Advertisement
deleted user

Re: ob_flush pushes output before Joomla header

Post by deleted user » Mon Sep 30, 2019 10:16 pm

Joomla output is wrapped in buffers in a number of places - https://github.com/joomla/joomla-cms/se ... q=ob_start

You need to ensure you are not closing an already active buffer otherwise that will cause unintended side effects such as those you are seeing.

chasiv
Joomla! Intern
Joomla! Intern
Posts: 59
Joined: Sat Feb 03, 2007 3:02 am
Location: Peru, ME USA
Contact:

Re: ob_flush pushes output before Joomla header

Post by chasiv » Mon Sep 30, 2019 10:26 pm

So how do I do this without effecting the existing Joomla buffers?

chasiv
Joomla! Intern
Joomla! Intern
Posts: 59
Joined: Sat Feb 03, 2007 3:02 am
Location: Peru, ME USA
Contact:

Re: ob_flush pushes output before Joomla header

Post by chasiv » Mon Sep 30, 2019 11:13 pm

Ok.. if I comment out the following:

Code: Select all

while (@ob_end_flush());
it works as expected, but the page takes forever to load. Any thoughts on how to resolve that? This is the entire page script.

CORRECTION: then it does not load properly.. everything on the page is loaded and displayed all at once (defeating the entire purpose)

deleted user

Re: ob_flush pushes output before Joomla header

Post by deleted user » Wed Oct 02, 2019 8:08 pm

If what you're trying to do is in effect send partial HTML chunks to the browser until the full document is rendered, then this isn't a good way to approach the problem. It sounds like the better approach is going to be having some kind of JavaScript fire on page load so your tasks can be done in follow-on AJAX requests instead of blocking the page response.

chasiv
Joomla! Intern
Joomla! Intern
Posts: 59
Joined: Sat Feb 03, 2007 3:02 am
Location: Peru, ME USA
Contact:

Re: ob_flush pushes output before Joomla header

Post by chasiv » Wed Oct 23, 2019 2:42 pm

Anyone have an answer to how to ob_flush without breaking Joomla? I am trying to use a module and post updates to the page. Using AJAX to populate a form with query results is a pain when there are multiple queries and calls.

deleted user

Re: ob_flush pushes output before Joomla header

Post by deleted user » Thu Oct 24, 2019 7:43 pm

You're going to want some kind of JavaScript based component in your workflow. That's the right way to deal with it.

Joomla wraps output in buffers to keep from running into funny issues with sending response headers at random spots in the code (amongst other reasons). So just randomly trying to flush/echo out all of Joomla's active buffers, then echo out your partial HTML chunks, then reset the Joomla state so it correctly closes out the request isn't a viable option, and in fact is probably prone to errors and easily breaking.

An AJAX based solution is going to call for the same number of database queries as it takes to do your handling in the same request as the full HTML document. So the pain point is going to be in converting the AJAX response from the server into the right format to place your data into the DOM (which is the exact type of thing that single page applications and JavaScript frameworks like Vue and Angular do).

I really don't know what you're trying to do, but the approach you're insisting upon just is not a sound solution.

Advertisement

Locked

Return to “Joomla! 3.x Coding”