geester Posted March 2, 2010 Share Posted March 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/ Share on other sites More sharing options...
jacko310592 Posted March 2, 2010 Share Posted March 2, 2010 php is server side, as far as i know it only displays once the operation has completed, your going to need javascript to complete what you want... that or page reloads which show different infomation at different stages Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020475 Share on other sites More sharing options...
geester Posted March 2, 2010 Author Share Posted March 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020479 Share on other sites More sharing options...
tibberous Posted March 2, 2010 Share Posted March 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020484 Share on other sites More sharing options...
jacko310592 Posted March 2, 2010 Share Posted March 2, 2010 i dont actually have any experiance with the function your using, but my guess is that your host is blocking the process, if no one can help you on here, i suggest just contacting your host and ask them if they are preventing it from working correctly. Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020485 Share on other sites More sharing options...
salathe Posted March 2, 2010 Share Posted March 2, 2010 Check the configuration of the two servers for differences, particularly the output buffer configuration. Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020491 Share on other sites More sharing options...
geester Posted March 2, 2010 Author Share Posted March 2, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020492 Share on other sites More sharing options...
geester Posted March 2, 2010 Author Share Posted March 2, 2010 Check the configuration of the two servers for differences, particularly the output buffer configuration. Yep, I've been messing with this all day, all servers were set to output_buffering = Off Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020493 Share on other sites More sharing options...
teamatomic Posted March 2, 2010 Share Posted March 2, 2010 for($i=0;$i<10;$i++) { ob_start(); echo "$i<br>"; flush(); ob_end_flush(); sleep(1); } HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020495 Share on other sites More sharing options...
geester Posted March 2, 2010 Author Share Posted March 2, 2010 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 for($i=0;$i<10;$i++) { ob_start(); echo "$i<br>"; flush(); ob_end_flush(); sleep(1); } HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020499 Share on other sites More sharing options...
teamatomic Posted March 2, 2010 Share Posted March 2, 2010 Show the actual code HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020506 Share on other sites More sharing options...
geester Posted March 2, 2010 Author Share Posted March 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020509 Share on other sites More sharing options...
teamatomic Posted March 2, 2010 Share Posted March 2, 2010 Then it might have to do with server configuration. What does phpinfo() say about output buffering in the first block(core). HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020511 Share on other sites More sharing options...
geester Posted March 2, 2010 Author Share Posted March 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020517 Share on other sites More sharing options...
teamatomic Posted March 2, 2010 Share Posted March 2, 2010 The fact that is shows "no value" is fine. It means you have to set it in your script. I really have no further ideas as to why you are experiencing the erratic behavior with the loop. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020538 Share on other sites More sharing options...
geester Posted March 2, 2010 Author Share Posted March 2, 2010 OK, Thanks everyone for your help. Appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020539 Share on other sites More sharing options...
teamatomic Posted March 2, 2010 Share Posted March 2, 2010 There was something tickling the back of my mind and it just came through. Do the servers that fail use gzipped output? if they do it will zip it all up and throw it to the browser so you wont see the delayed output. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020540 Share on other sites More sharing options...
geester Posted March 2, 2010 Author Share Posted March 2, 2010 Ah, I'm seeing this in the SERVER_SOFTWARE section of phpinfo: mod_gzip/1.3.26.1a - This isn't on the server that works. Any idea how to disable this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020549 Share on other sites More sharing options...
geester Posted March 2, 2010 Author Share Posted March 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020568 Share on other sites More sharing options...
teamatomic Posted March 2, 2010 Share Posted March 2, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020573 Share on other sites More sharing options...
geester Posted March 2, 2010 Author Share Posted March 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020580 Share on other sites More sharing options...
geester Posted March 2, 2010 Author Share Posted March 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193911-output-list-line-by-line/#findComment-1020615 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.