PHP echo antes de la función de suspensión, no funciona

I want output echo in browser (every time) before sleep function execute.

following code is not working

    set_time_limit(0);
    ob_implicit_flush(1);
    ob_start();
    echo "Start<Br>";
    ob_flush();

    for($i=0;$i<10;$i++){
        $randSlp=rand(1,3);
        //echo str_repeat(" ", 1024);
        echo "Sleeping for ".$randSlp." second. ";
        ob_flush();

        sleep($randSlp);
    }
    ob_end_flush(); 

si descomentar str_repeat function than in browser
First time : Start Sleeping for 1 second. Sleeping for 3 second.
Second time : Sleeping for 2 second. Sleeping for 2 second.

y continuar ...

is possible echo one by one without str_repeat() function, why output doesn't display every time.

preguntado el 23 de abril de 13 a las 14:04

You may need to flush your output buffer. ob_flush()? -

@J.D.Pace I am using ob_flush(); after echo, or need to put other place -

2 Respuestas

Try following code and its work.

header( 'Content-type: text/html; charset=utf-8' );
header("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
set_time_limit(0);
ob_implicit_flush(1);
//apache_setenv('no-gzip', 1);
//ini_set('zlib.output_compression', 0);
//ini_set('implicit_flush', 1);
for ($i = 0; $i < 10; $i++) { 
    $randSlp=rand(1,3);
    echo "Sleeping for ".$randSlp." second. ";;
    sleep(1);
    if(ob_get_level()>0)
       ob_end_flush(); 
}

Respondido 25 Abr '13, 06:04

What is the problem and how does your code solve it? - showdev

even the output buffer (ob_* functions) do not necessarily give output to browser directly.

First try calling flush() antes o después ob_flush().

Second, look if mod_gzip or zlib.output_compression is turned on for example. This will also buffer all output.

If using IIS server and not Apache, there might also be settings in IIS to check.

Respondido 23 Abr '13, 14:04

Lo intenté flush() antes y después ob_flush() its not work. - RavatSinh Sisodiya

@RSSisodiya Ok, so as I said, chkeck mod_gzip and zlib.output_compression. Do phpinfo() and check if _SERVER["HTTP_ACCEPT_ENCODING"] does gzip and check if zlib.output_compression is turned on. These will block direct outputting - nl-x

$_SERVER["HTTP_ACCEPT_ENCODING"]. return gzip, deflate. - RavatSinh Sisodiya

and i have set ini_set("zlib.output_compression", "On"); (in my php file) but still not working - RavatSinh Sisodiya

@RSSisodiya Try turning it OFF :). To compress involves buffering everything first before being able to compress it. - nl-x

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.