-1

strange issue, this script works on my local but not on my shared php hosting...

while (1 <20) {
    $time = time();
    echo "data: {$time}\n\n";
    ob_flush();
    flush();

    sleep(3);
  }

but if I comment out the while loop, it works fine. Anyone aware of this strange issue?

Error seen in chrome is - "Provisional headers are shown" (if I comment out the while loop, it works fine)

Just to note...I know that's an infinite loop.

Luckyy
  • 1,021
  • 4
  • 15
  • 29
  • so you think `$time = time();` is causing the issue? doesn't seem on this end – Kevin Sep 14 '14 at 11:47
  • No, that is working fine on local...and on the server if I comment out the loop – Luckyy Sep 14 '14 at 11:48
  • maybe related http://stackoverflow.com/questions/21177387/caution-provisional-headers-are-shown-in-chrome-debugger – Kevin Sep 14 '14 at 11:49
  • Its not related, as I said above if I comment out the loop it works fine – Luckyy Sep 14 '14 at 11:50
  • well, the only one i could find related to your question is this http://stackoverflow.com/questions/10947908/how-to-echo-something-when-in-infinite-while-loop, if it still not related, then i wish you good luck – Kevin Sep 14 '14 at 11:53
  • 1
    If I had to guess, I'd say your shared host probably detects the infinite loop/high CPU utilization and kills the process so you don't bog down the server. Does the script run for any time at all? – Drew Hammond Sep 14 '14 at 11:54
  • @Luckyy I am agree with Drew. There are some restrictions for how many time a loop will be allowed. – Amit Garg Sep 14 '14 at 11:56
  • Ok then let me try, and will let you know – Luckyy Sep 14 '14 at 11:57
  • ok then I tried with a small loop, answer is PHP gives response back only after script finished running including finishing the while loop, so its related to the second link ghost posted – Luckyy Sep 14 '14 at 12:02
  • What do you mean it works? You can never actually load a page containing this script since it loops for ever and never renders! Infinite loops are only useful for background processes or listeners. Read this please: http://stackoverflow.com/questions/1765733/when-are-infinite-loops-are-useful-in-php – Arijoon Sep 14 '14 at 12:19
  • Yes Arijoon, that's what I was doing, I am using html5's eventsource and this while loop actually is a listner that pushes data every 3 seconds – Luckyy Sep 14 '14 at 12:31

1 Answers1

0

I think I know your issue.

Using ob_flush() and flush() in combination can be hit and miss.

However, it might work if you define a header before the loop, like this:

header( 'Content-type: text/html; charset=utf-8' );
kmoe
  • 1,963
  • 2
  • 15
  • 27
Juddus
  • 1
  • 1
  • hi Juddus, the answer is PHP gives response back only after script finished running including finishing the while loop...thanks :) – Luckyy Sep 15 '14 at 15:24