ericc
ericc

Reputation: 781

Output line by line with PHP

In a small application, I'm writing, during installation phase, the user must enter detail about the MySQL connection in a form (server name, database name, username ...)

I would like to display line by line the result of tests and actions done in a window, with eventually a small pause between each test, like :

"Connection to the server ..."(2s) "ok" (2s)
"Connection to the database ..."(2s) "ok" (2s)
"Creation of tables ..."(2s) "ok"
....etc

I have tried with ob_start(), flush(), ob_flush() but it doesn't work as my webserver seems to buffer itself the php output, so the script takes a long time to run but everything is printed at the same time.

I have searched here and with Google, without any result (may be not with the correct keywords)

Can you point me to solution ?
with Ajax may be ?

ericc

Upvotes: 0

Views: 1870

Answers (2)

gnur
gnur

Reputation: 4733

There are multiple solutions to this, but disabling caching altogether isn't easily accomplished.
Because on the server side there is some caching, which can be disabled, but there is also a very large chance that the browser is caching as well.

To fix this you have a few options:

  • fake it, display a static javascript based "animation" and redirect when done.

  • Use ajax, let the installation script echo the results to a text file / mysql database and use a different php script to load the the text file / database at set intervals and display the new results.

  • Use the newest HTML5/javascript streaming API which is made exactly for stuff like this, it is, however, quite hard to find any good documentation on this.

To disable server side caching, I use this code:

@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);
header('Expires: Fri, 01 Jan 1990 00:00:00 GMT');
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Pragma: no-cache');
header('Connection: close');

Upvotes: 1

user743234
user743234

Reputation:

Have you tried usleep() before every time printing out?

Edit

You can use usleep() to make your php script delay the execution so that texts can be seen one by one, it could substitute the use of buffering IN YOUR CASE.

mysql_connect() successfully -> print something -> usleep(300)
mysql_select_db() successfully -> print something -> usleep(300)

Upvotes: 1

Related Questions