TripleDES Posted May 27, 2007 Share Posted May 27, 2007 Very simple script. When executed from CLI, I get desired response. Each character is displayed while it iterates. ie. 1..2..3 However, when running this script through a browser, it waits before the script completes before displaying 123. Any ideas why? Someone in a PHP newsgroup mentioned possible output buffering enabled. I since disabled this, but still seem to get the same issue. <?php while($i++<3) { print($i); sleep(2); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53216-scripts-waits-until-complete-before-displaying-results/ Share on other sites More sharing options...
AndyB Posted May 27, 2007 Share Posted May 27, 2007 'waits until it completes ...' - exactly how php normally works. The script executes on the server and when completed (including the sleeps) the output is sent to the (viewer's) browser to be rendered. Quote Link to comment https://forums.phpfreaks.com/topic/53216-scripts-waits-until-complete-before-displaying-results/#findComment-262914 Share on other sites More sharing options...
dj-kenpo Posted May 27, 2007 Share Posted May 27, 2007 PHP is not C++. as ANdy said, it's doing what PHP does, ther eis no workaround, you want flash or javascript, not php Quote Link to comment https://forums.phpfreaks.com/topic/53216-scripts-waits-until-complete-before-displaying-results/#findComment-262920 Share on other sites More sharing options...
AndyB Posted May 27, 2007 Share Posted May 27, 2007 Actually, there is a 'workaround' - but it's really a normal part of php so calling it a workaround isn't right. flush Give this a test: <?php ob_start(); for ($i=1;$i<10;$i++) { echo $i. "<br/>"; sleep(1); // pause 1 second ob_flush(); flush(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53216-scripts-waits-until-complete-before-displaying-results/#findComment-262923 Share on other sites More sharing options...
TripleDES Posted May 27, 2007 Author Share Posted May 27, 2007 Thanks for the quick response. So how do I defer from this default behavior? Here's what I'm going with this script. I want to form a ping script that will ping x host x times. Let's say I want to ping 4.2.2.2 20 times. I would like a running output: PING 4.2.2.2 (4.2.2.2) 56(84) bytes of data. 64 bytes from 4.2.2.2: icmp_seq=1 ttl=247 time=139 ms 64 bytes from 4.2.2.2: icmp_seq=2 ttl=247 time=147 ms 64 bytes from 4.2.2.2: icmp_seq=3 ttl=247 time=168 ms 64 bytes from 4.2.2.2: icmp_seq=4 ttl=247 time=143 ms ...etc. Currently, the user must wait until the script is complete to see the entire output. Quote Link to comment https://forums.phpfreaks.com/topic/53216-scripts-waits-until-complete-before-displaying-results/#findComment-262924 Share on other sites More sharing options...
TripleDES Posted May 27, 2007 Author Share Posted May 27, 2007 PHP is not C++. as ANdy said, it's doing what PHP does, ther eis no workaround, you want flash or javascript, not php hmm...no workaround? that's unfortunate. Quote Link to comment https://forums.phpfreaks.com/topic/53216-scripts-waits-until-complete-before-displaying-results/#findComment-262925 Share on other sites More sharing options...
AndyB Posted May 27, 2007 Share Posted May 27, 2007 um, did you try the code I posted?? Quote Link to comment https://forums.phpfreaks.com/topic/53216-scripts-waits-until-complete-before-displaying-results/#findComment-262927 Share on other sites More sharing options...
TripleDES Posted May 27, 2007 Author Share Posted May 27, 2007 um, did you try the code I posted?? Apologies, I posted before seeing your response. See my partial script below. The location of the output buffering essentially has an artificial effect of what I'm trying to do. The script actually completes, but then has a 1 second delay before displaying each line. if (preg_match("$regex", $ping_ip_addr) OR preg_match("$regex2", $ping_ip_addr)) { exec($ping, $result); ob_start(); foreach($result as $val) { print "$val<br>"; sleep(1); ob_flush(); flush(); } Quote Link to comment https://forums.phpfreaks.com/topic/53216-scripts-waits-until-complete-before-displaying-results/#findComment-262937 Share on other sites More sharing options...
TripleDES Posted May 29, 2007 Author Share Posted May 29, 2007 I was able to get output buffering working as expected with other scripts. However, it seems when I try to run an external program such as ping, PHP waits until the command completes before releasing any output. $ping = "ping -i .25 -c $ping_count -s $ping_size $ping_ip_addr"; I've tried exec(), passthru() and plan 'ping x.x.x.x' to no avail. Any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/53216-scripts-waits-until-complete-before-displaying-results/#findComment-264349 Share on other sites More sharing options...
TripleDES Posted June 15, 2007 Author Share Posted June 15, 2007 Anyone else want to take a crack at this? Quote Link to comment https://forums.phpfreaks.com/topic/53216-scripts-waits-until-complete-before-displaying-results/#findComment-275280 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.