Jump to content

Recommended Posts

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);
}
?> 

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();
}
?>

 

 

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.

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();
  }

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?

  • 3 weeks later...
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.