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>';
}
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. :-)
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!
精彩评论