0

For example I have something like this :

<?php
  header("Content-Type: text/html; charset=UTF-8");
  set_time_limit(0); 
  ob_start("ob_gzhandler");

  while(true) :
    echo microtime(true)."<br>";
    ob_flush();
    flush(); 
    sleep(1);
  endwhile;

  ob_end_clean();
?>

This code works on my localhost and each second on the page is printed microtime(), but when I try to run same script on my Shared Linux Hosting nothing is printed, page just has infinite loading time.

How to print something when in infinite loop, on my hosting?

Maybe I have to enable /disable something in my php.ini file? any Ideas?

Machavity
  • 30,841
  • 27
  • 92
  • 100
John
  • 7,500
  • 16
  • 62
  • 95

1 Answers1

2

It's Gzip waiting for all the data, so it can compress and send it.

As you're on shared hosting, it might be a bit tricky to disable this completely. So we can either:

Disable Gzip on the Linux hosting by using either PHP

ini_set('output_buffering','on');
ini_set('zlib.output_compression', 0)

Disable Gzip by using .htaccess

SetEnv no-gzip dont-vary



EDIT: Could you also try this out on your Linux Host?

<?php
ini_set('output_buffering','on');
ini_set('zlib.output_compression', 0);
ob_implicit_flush();
for($i=0;$i<100;$i++) {
      echo $i;
      echo str_repeat(" ", 500);
      ob_flush();
      flush();
      sleep(1);
}
?>

This one works on my host, it'd be interesting to see if it works on yours.

Ben Poulson
  • 3,368
  • 2
  • 17
  • 26
  • I edited and checked my php5.ini file and added that line to .htaccess, but it still don't display anything – John Jun 08 '12 at 11:41
  • Have a look at my edit, I posted a working piece of code I just tested out on my host. – Ben Poulson Jun 08 '12 at 11:47
  • It displays results only after page is loaded. – John Jun 08 '12 at 11:53
  • I've got the same issue on a GoDaddy shared hosted web server and (unfortunately), this solution does not work for me. ob_flush() and flush() are completely ineffective. Output is not displayed until the script finishes execution. I only have issues on this one server (the only shared hosted server I use). I tried both of the solutions in your answer (the ini_set() function calls and the .htaccess file). The same problem still occurs. While I cannot guarantee I will keep this page up, here is the URL for the page where it happens: http://www.specpro.us/.admin/specpro.us.reqcert.php?debug=1 – chriv Mar 17 '16 at 16:44