0

I have in my index.php file

<?php
    ob_start();
    echo '<html>
              <head>
                  <style>
                      body {background-color: black; color: white}
                  </style>
              </head>
              <body>
                  <h2 id="greeting">Wait for page load</h2>
              </body>
          </html>';
    ob_flush();
    flush();
    sleep(100);
    echo '<script>document.getElementByID("greeting").innerHTML = "Page loaded!";</script>';
    ob_flush();
    flush();
?>

I thought it will send the HTML content to the client and they will see a "Wait for page load" text because it is flushed out, but on my website the webpage is just a blank white screen until the PHP code has finished executing (after 100 seconds) then everything on the page displays at once.

I have tried adding

echo str_repeat("<!--AAAAAAAAAAAA-->", 100);

after each echo as well to make sure it starts sending blocks of data to the browser but that didn't work either.

Is there a way for me to display the HTML content from the php file before the php code finishes executing?

Thanks!

EDIT: Everyone is telling me to use ob_start(); ob_flush(); and flush(); but I have used it in the code above already?

Friedpanseller
  • 654
  • 3
  • 16
  • 31
  • there's a small problem though, you're adding the script outside of your html tags... – Florian Humblot Jun 20 '17 at 08:25
  • Possible duplicate of [PHP doesn't show content before fully loaded](https://stackoverflow.com/questions/44647429/php-doesnt-show-content-before-fully-loaded) – Carl Binalla Jun 20 '17 at 08:26
  • `sleep(100);` hence it display after 100 seconds – Masivuye Cokile Jun 20 '17 at 08:26
  • Check [this](https://stackoverflow.com/questions/4706525/php-flush-not-working) and [this](https://stackoverflow.com/questions/13751772/php-output-buffer-not-flushing) - maybe the answers will help you. It could be a configuration problem. – CodeBrauer Jun 20 '17 at 08:27
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – CD001 Jun 20 '17 at 08:27
  • `` is part of `` – Masivuye Cokile Jun 20 '17 at 08:28
  • Also consider maybe using ajax for longer loading pages, which gives you ton of more options. – CodeBrauer Jun 20 '17 at 08:28
  • Possible duplicate of [Printing results immediately (php)](https://stackoverflow.com/questions/1363600/printing-results-immediately-php) – CBroe Jun 20 '17 at 08:28
  • https://stackoverflow.com/questions/44647429/php-doesnt-show-content-before-fully-loaded – CBroe Jun 20 '17 at 08:29
  • do you use apache or nginx ? and take in mind the note about mod_gzip 'Server modules for Apache like mod_gzip may do buffering of their own that will cause flush() to not result in data being sent immediately to the client. ' – hassan Jun 20 '17 at 08:38

2 Answers2

3

you need rather removing the ob_start from your script, or if ob_start is required within your program logic you may need to use ob_end_flush to

Flush (send) the output buffer and turn off output buffering

as follows:

<?php
ob_start();
echo '<html>
          <head>
              <style>
                  body {background-color: black; color: white}
              </style>
          </head>
          <body>
              <h2 id="greeting">Wait for page load</h2> '. time() .'
          </body>
      </html>';

ob_end_flush(); // <--------------

ob_flush();
flush();
sleep(2);
echo '<script>document.getElementById("greeting").innerHTML = "Page loaded!";</script>';
ob_flush();
flush();
?>
Shivam Sharma
  • 1,277
  • 15
  • 31
hassan
  • 7,812
  • 2
  • 25
  • 36
  • OMG THANK YOU SO MUCH THIS WORKED I just needed the ob_end_flush(). Was getting hopeless after seeing all those other pepoles' comments – Friedpanseller Jun 20 '17 at 09:16
0

The right way to do what you are trying to accomplish is using ajax. Using output buffering in this case is unreliable, because you may have other buffering layer other than yours.

I suggest you to read this good Q&A: PHP buffer ob_flush() vs. flush()

TL;DR Your web server might itself implement a buffering scheme (mod_deflate or content filter), which you have no influence over.

salvatore
  • 511
  • 4
  • 11