简体   繁体   中英

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM