Jump to content

output list line by line


geester

Recommended Posts

Hi, I am struggling to get the following to send the output to the browser line by line, instead it is waiting until it has completed and displaying it all at once. The code works fine on one of my servers but unfortunately it's not the server I need to use.  The server is running php 4.4.9. Any ideas please? :

 

<?php
ob_start();

for($i=0;$i<10;$i++)
{
    echo 'printing...<br />';
    ob_flush();
    flush();

    usleep(500000);
}

?>

 

Thanks.

Link to comment
Share on other sites

Thanks for the quick reply. This does work though on one of my servers, just can't figure out why it doesn't work on this one.

 

http://www.100pounddomains.co.uk/flush.php - works here

 

http://www.b4.co.uk/flush.php - doesn't work here

 

If I can't figure it out does anyone have any javascript ideas that would solve this please?

 

Thanks.

 

 

 

Link to comment
Share on other sites

Not sure how to get that to work - stuff like that was generally done back before browsers were capable of doing much. One of the problems with doing weird stuff like that is that it can easily be broken by server settings (timeout issues), browser versions, php versions, ect.

 

If you have the time, you'd probably do better to write it with ajax. If your sleep is really long, you could even do it with straight meta refreshes and a session/db/file.

 

 

Link to comment
Share on other sites

Yep, it's a weird one and I'm guessing must be down to the php setup somewhere. I have 3 servers with three different hosts, all with different php versions:

 

4.4.9 - code doesn't work

5.2.5 - code doesn't work

5.2.9 - code works.

 

I don't think it's the host. If I remove the ob_ and flush parts from the above code and just run the for loop it still prints out line by line on the 5.2.9 server.

 

I have zero experience with ajax/javascript. Looks like I need to do some reading :)

 

 

Link to comment
Share on other sites

Thanks, that didn't fix the issue though unfortunately. I've been playing around with ob_flush, ob_end_flush, flush all day in every sort of combination and order I can think of and none of them seem to make a difference.

 

As I said above, if I just run the FOR loop on the 5.2.9 server it outputs the list line by line without using any buffer code at all. Proper confused  :confused:

 

 

for($i=0;$i<10;$i++)

{

ob_start();

echo "$i<br>";

flush();

ob_end_flush();

sleep(1);

}

 

 

HTH

Teamatomic

Link to comment
Share on other sites

The original code is in my first post.

 

This is what I'm using with the buffer stuff commented out:

 

<?php
// ob_start();

for($i=0;$i<10;$i++)
{
    echo 'printing...<br />';
    // ob_flush();
    // flush();

    usleep(500000);
}

?>

 

Both versions print out line by line on the 5.2.9 server but wait until completion on the other 2.

Link to comment
Share on other sites

Then it might have to do with server configuration. What does phpinfo() say about output buffering in the first block(core).

 

On all servers phpinfo shows: output_buffering  no value - looking at php.ini the actual line is output_buffering = Off

 

I've tried comparing the phpinfo files on each sever but don't really know what I'm looking for to be honest.

Link to comment
Share on other sites

You're an absolute star :)

 

Not sure if it's the best option but putting this in .htaccess has partly fixed the issue:

 

<IfModule mod_gzip.c>

mod_gzip_on no

</IfModule>

 

Just left with the issue that IE still waits for completion on the first visit to the page but prints everything line by line when refreshing the page??

 

Thanks.

 

 

 

Link to comment
Share on other sites

There is something again tickling back there about IE and self caching up to 256 bytes before output. A google should turn up something to work around it as this is a known issue. I would say to output 250 bytes of white space which the browser should count but not display.

 

 

HTH

Teamatomic

Link to comment
Share on other sites

:) it's certainly related to the 256 issue, have put some code on the test page which has fixed the issue with IE. Unfortunately the same code doesn't seem to fix the issue on the real code but think I'll leave that for tommorrow!

 

Many thanks for your help, greatly appreciated.

Link to comment
Share on other sites

Just a further update on this in case it helps someone else out. The fixes above, turning off gzip and adding whitespace to the output do fix the problem.

 

My data is being output in a table, in this case it seems that IE will not flush the data until it has at least 256 bytes of data AND it gets to the closing </table> tag.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.