ob_start() not working

Discussion in 'PHP' started by bandito40, Jun 29, 2008.

  1. #1
    Hi, I am using the following code to send a period to the browser ever second but all periods are sent at once 6 seconds later. Anyone know how I could get this script to display a one period per second?


    ob_start();
    while($i < 6){
    echo ".";
    $i++;
    sleep(1);
    ob_end_flush();
    }
    ob_end_clean();

    Thanks
     
    bandito40, Jun 29, 2008 IP
  2. xfreex

    xfreex Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    ob_start();
    while($i < 6){
    echo ".";
    sleep(1);
    $i++;


    }
    ob_end_flush();
    ob_end_clean();
     
    xfreex, Jun 29, 2008 IP
  3. bandito40

    bandito40 Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think you miss understood my objective. I would like to have a period printed to the browser once per second and not 6 periods 6 seconds later. If ob_flush() or ob_end_flush() is called with in the loop then I get the desired results however it only works in CLI and not in the browser.

    Thanks
     
    bandito40, Jun 29, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    My preferred method:

    for ( $i = 0; $i < 6; $i++ )
    {
    echo '.';
    sleep(1);
    ob_flush();
    flush();
    }
     
    Danltn, Jun 29, 2008 IP
  5. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #5
    That's exactly how I'd do it. Note you may have to output up to 1024 bytes before this, as some browsers (I can't remember which now), don't follow this kind of sequence until a minimal amount of data has passed.

    Jay
     
    jayshah, Jun 29, 2008 IP
  6. chaosprime

    chaosprime Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You might also want to look into ob_implicit_flush().
     
    chaosprime, Jun 29, 2008 IP
  7. bandito40

    bandito40 Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Actually, based on the post by Danltn and a post in another forum, adding flush(); after ob_end_flush() or after ob_flush() will do the trick in both FireFox 2 and IE 6.

    Thanks for everyone's help.
     
    bandito40, Jun 29, 2008 IP