Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts

    flush() - why the heck isn't this working?

    Hey guys, any idea why this isn't working?
    The output just dumps all to the browser, instead of 1 second increments as l would expect.

    PHP Code:
    <?

    ob_end_flush
    ();  
    flush();

    for(
    $i=0;$i<5;$i++){
        echo 
    'test ' $i '<br />';
        
    flush();
        
    sleep(1);
    }

    ?>

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    There's a lot of buffering going on (gz handler, apache etc). Basically, this isn't going to ever work unless you have a very specific and unusual setup (and one that's far from optimal).
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,441
    Thanks
    62
    Thanked 537 Times in 524 Posts
    Then you get ISP caching...

    Plus the socket buffer sizes on your computers OS.. there are so many different buffers between your browser and the script on the server that you'll never achieve the desired result. The only other thing you can do is to repeatedly call the script time and time again Not ideal but it would have the desired effect.. ish.

    Basically, unless its a data stream, you can't use php for a live output. Thats because most socket buffers have a size of around 4Kb give or take a few.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    remove / comment out

    ob_start();
    ob_end_flush();

    etc if they appear anywhere

    add this to a functions file or somewhere it can be referenced

    PHP Code:
    /** 
    * Output buffer flusher
    * Forces a flush of the output buffer to screen useful for displaying long loading lists eg: bulk emailers on screen 
    * Stops the end user seeing loads of just plain old white and thinking the browser has crashed on long loading pages.
    */
    function fcflush()
    {
        static 
    $output_handler null;
        if (
    $output_handler === null) {
            
    $output_handler = @ini_get('output_handler');
        }
        if (
    $output_handler == 'ob_gzhandler') {
            
    // forcing a flush with this is very bad
            
    return;
        }
        
    flush();
        if (
    function_exists('ob_flush') AND function_exists('ob_get_length') AND ob_get_length() !== false) {
            @
    ob_flush();
        } else if (
    function_exists('ob_end_flush') AND function_exists('ob_start') AND function_exists('ob_get_length') AND ob_get_length() !== FALSE) {
            @
    ob_end_flush();
            @
    ob_start();
        }

    call your code

    and replace flush() with fcflush();
    PHP Code:
    <?php

    for($i=0;$i<5;$i++){
        echo 
    'test ' $i '<br />';
        
    fcflush();
        
    sleep(1);
    }

    ?>
    and for gods sake use proper php openings not short openings.
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,441
    Thanks
    62
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by hinch View Post
    add this to a functions file or somewhere it can be referenced

    PHP Code:
    /** 
    * Output buffer flusher
    * Forces a flush of the output buffer to screen useful for displaying long loading lists eg: bulk emailers on screen 
    * Stops the end user seeing loads of just plain old white and thinking the browser has crashed on long loading pages.
    */
    function fcflush()
    {
        static 
    $output_handler null;
        if (
    $output_handler === null) {
            
    $output_handler = @ini_get('output_handler');
        }
        if (
    $output_handler == 'ob_gzhandler') {
            
    // forcing a flush with this is very bad
            
    return;
        }
        
    flush();
        if (
    function_exists('ob_flush') AND function_exists('ob_get_length') AND ob_get_length() !== false) {
            @
    ob_flush();
        } else if (
    function_exists('ob_end_flush') AND function_exists('ob_start') AND function_exists('ob_get_length') AND ob_get_length() !== FALSE) {
            @
    ob_end_flush();
            @
    ob_start();
        }

    Not that socket buffer sizes on a windows machine or an ISP can be overridden by PHP but you're still using flush() in your function - which as demonstrated already doesn't work.

    Secondly I've just tried using it on my wamp setup and again, it doesn't work through a browser. I did however get it to work up to the 4th loop using a http debugging program.. which then froze. I tried another which did work upto and past 30 however no normal browser will work like this.

    Finally if it doesn't work on localhost it certainly won't on a remote server with an ISP between it and the end user due to cache issues and socket buffers.

    To put it mildly, your code is unstable.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #6
    New Coder
    Join Date
    Sep 2011
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    as far as i know, the web browser do not display any part of the page untill it received the full file.

    and the load images and js files

  • #7
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    Quote Originally Posted by tangoforce View Post
    To put it mildly, your code is unstable.
    The code is very very stable and used in several very large scale commercial projects where its tried and tested. (This very forum uses exactly the same code snippet!)

    Not only does it work perfectly on wamp servers but it also works perfectly on unix servers.

    If a page flush isn't happening then there's some other issues at work which aren't browser/code related.

    In usage you'd use it something like this.

    PHP Code:
    while (($data fgetcsv($handle3000",")) !== FALSE) {
    set_time_limit(60); //reset time limit to stop script timing out
    echo "<span style=\"color:green\">$row - Importing Ticket ".$data[4]."</span> - ";
    $row++;
    //check if ticket already exists
    $sql "";
    $result mysql_query($sql) or die(mysql_error());
                        if (
    mysql_num_rows($result)>0) {
                            echo 
    "Ticket Exists - Updating<br />";
                            
    $importsql "";
                            
    mysql_query($importsql) or die(mysql_error());
                            
    fcflush();
                        } else {
                            
    $importsql "";
                            
    mysql_query($importsql) or die(mysql_error());
                            echo 
    "New Ticket - Inserting<br />";
                            
    fcflush();
                        }
                    } 
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,441
    Thanks
    62
    Thanked 537 Times in 524 Posts
    hinch, I'd invite you to demonstrate it in use but to be honest, I've never once seen any flush call work successfully in a browser.

    As I said, for some http based programs such as the one I used yesterday it may work but for webpages it does not.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #9
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    i shall send you a PM to a small video
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •