Linksku
Linksku

Reputation: 1459

PHP: How can I output HTML while the script is still running?

I have a script that checks the status of a few hundred webpages. However, the script takes about 2 minutes to load, and the screen is blank until the script has finished running. Then all the data is outputted at once.

I want to output data while the script is still running. Here's part of my script:

foreach ($urls as $url){

    $headers = get_headers($url,true);
    $status = $headers[0];
    list($protocol, $code, $message) = explode(' ',$status,3);

    echo '<br>'.$url.'<br>'.$code.'<br>';

} 

Upvotes: 3

Views: 2415

Answers (2)

James
James

Reputation: 5169

This is a common function which I use throughout a lot of my scripts to see progress.

function flush_buffers() { 
    ob_end_flush(); 
    ob_flush(); 
    flush(); 
    ob_start(); 
}

I can't remember the original source of this function, probably from php.net somewhere!

Hope it helps!

Upvotes: 2

Caladain
Caladain

Reputation: 4934

http://php.net/manual/en/function.flush.php contains the answer you seek.

Be aware this may negatively hit your performance, but it sounds like you're more interested in seeing it's progress. :-)

Upvotes: 2

Related Questions