Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Flush/ob_flush not working

I've tried several attempts at getting my flush and ob_flush to work. I've tried setting the ini to allow buffering, I've tried using several different functions I found online for output buffering, and none of it at all is working. The script wants to wait until it is completly done until it echos output. Here is the script I have so far

 ob_start();

 //Login User
 echo 'Logging in to user<br>';
       ob_flush();
       flush();
      $ch = curl_init("http://www.mysite.com/login/");
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$user&pass=$pass");
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies/$cookie");
      curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies/$cookie");
      $output = curl_exec($ch);
      curl_close($ch);
      ob_flush();
      flush();

       //Update Status
 echo 'Updating Status<br>';
       ob_flush();
       flush();
      $ch = curl_init("http://www.mysite.com/update/");
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, "status=$status");
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies/$cookie");
      curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies/$cookie");
      $output = curl_exec($ch);
      curl_close($ch);
      ob_flush();
      flush();

I want it to echo what it is doing, then run the function, then echo something else, then do another function. I want all the buffers to be flushed and echoed in real time on the browser.

like image 648
JakeSmith Avatar asked Dec 19 '10 02:12

JakeSmith


People also ask

How to flush output of PHP?

Flush() sends all output out immediately, without waiting for the end of the script, and you can call it as often as you want. Calling flush() has the effect of making the browser update with new content.

What is flush () PHP?

The flush() function requests the server to send its currently buffered output to the browser. The server configuration may not always allow this to happen.

What is output buffer in PHP?

Output Buffering is a method to tell the PHP engine to hold the output data before sending it to the browser.

What is Ob_start?

The ob_start() function creates an output buffer. A callback function can be passed in to do processing on the contents of the buffer before it gets flushed from the buffer. Flags can be used to permit or restrict what the buffer is able to do.


1 Answers

I just wanted to write a quick note of what I've observed, now in 2016, of the different approached suggested:

The above codes offered by netcoder and David work for me in the following browsers:

  • Chrome
  • Opera

It does not seem to work in Firefox, Safari, or IE 10-11.

I've also tested the alternative code:

<?php

    if (ob_get_level() == 0) ob_start();
    for ($i = 0; $i<10; $i++){

        echo "<br> Line to show.";
        echo str_pad('',4096)."\n";    

        ob_flush();
        flush();
        sleep(2);
    }

    echo "Done.";

    ob_end_flush();
?>

Which can be found here: http://php.net/manual/en/function.flush.php#54841

Which seems to have better current support through all browsers:

  • Chrome
  • Firefox
  • Opera
  • Safari
  • IE 10
  • IE 11

The working implementations seem to change year to year, so I wanted to offer an update of what I've found myself to work at the moment.

like image 116
Prusprus Avatar answered Sep 21 '22 07:09

Prusprus