0

I am trying to implement the trick listed on this page http://developer.yahoo.com/performance/rules.html#flush "Flush the Buffer Early".

Everytime I try to run this thing I am not getting the desired output.

I have written the following code.

<html>
<head>
    <title>This is title</title>
    <script type="text/javascript" src="/1.js"></script>
    <link rel="stylesheet" type="text/css" href="/1.css">
</head>

<body>
ABC
<?php
flush();
sleep(3);
?>
</body>
</html>

The result enter image description here

I am getting the same result on both Firefox and Chrome.

What I expect is that the download of CSS and JS files should start immediately, and not wait for 3 seconds.

Based on the information given on the internet, I have tried the following things but nothing has helped.

1. ob_start(); and then ob_flush();

2. Using both ob_flush(); and flush(); ( in both the orders )

3. Adding the thing like this
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);

4. Putting more content in body 4~5 KB of content before flush.

5. And many other things.

I doubt if achieving this kind of thing is actually possible.

Manu
  • 901
  • 1
  • 8
  • 28
  • http://stackoverflow.com/q/352759/139010 – Matt Ball Jun 14 '13 at 18:10
  • You mean to say I should not do it? Going by the top answer to the post. I would like to inform you that I won't be doing G-Zip, I would only be combining & minifying the CSS and JS files. Also, in my case it generally takes 2-2.5 seconds to complete the entire PHP script + lot of images would also be present. doing this would mean 2 less HTTP requests. Also, I am not going to flush too often, I was planning to flush once after the `` and once after ` – Manu Jun 14 '13 at 18:25
  • are you behind another http server like nginx ? This question might be related: [PHP Flush that works… even in Nginx](http://stackoverflow.com/questions/4870697/php-flush-that-works-even-in-nginx) – Lepidosteus Jun 14 '13 at 21:38
  • @Lepidosteus.. No, I am just using Apache. – Manu Jun 15 '13 at 09:18
  • I am having the same issue now, @Manu, did you find a solution to this? This is extremely similar to the issue that I am facing! – Ninja Sep 14 '15 at 22:09
  • Sorry for the late reply. I was not able to find the solution to this.. So dropped this! – Manu Dec 03 '15 at 05:23
  • Did you happen find anything around it? Add as an answer if you found the solution. – Manu Dec 03 '15 at 05:24

2 Answers2

0

Hum... In yahoo's document, flush() was after </head> and before <body>.

  ... <!-- css, js -->
</head>
<?php flush(); ?>
<body>
  ... <!-- content -->

In your code, It's inside <body>. Did you tried to put the php code before it?

ldavid
  • 2,512
  • 2
  • 22
  • 38
0

Hi I faced the same situation it ob_start and ob_flush by itself did not work for me too.

so after editing the code it worked EDIT::

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

echo $i . '<br>';
sleep(1);
    echo str_pad('',4096) ;   
    ob_flush();
    flush();

}
?>  
Zeedia
  • 1,313
  • 14
  • 20
  • it is just my array so you can use your variables. And the loop because you need to display something in every sec. so can get rid of the loop but you need keep everything else. – Zeedia Jun 17 '13 at 14:36
  • This is of no help. :( I put a loop in which I had put sleep(1).. The loop ran 7 times. The loading of css,js files started only after 3 secs. Although its better that waiting for 7 secs. But still why a delay of 3 secs. Practically my script might not run for more that 2-3 secs. – Manu Jun 22 '13 at 18:56
  • I edited my code. It is working but you need to modify the sleep amount and the loop structure. – Zeedia Jun 24 '13 at 16:23
  • 1
    I have executed the same code as you have written.. Exact copy paste + 1 css and 1 js file in head section.. Its working fine. 1,2,3,4,5.. gets printed every second. But my question is that why doesn't the download of CSS and JS files start after the 1st flush? The download gets started only after 3 seconds. :( I have tried both Firefox and Chrome. – Manu Jun 24 '13 at 18:30