What is the recommended method for showing live logging on a web interface while a PHP script is running?

To show live logging on a web interface while a PHP script is running, you can use AJAX to periodically fetch and display logs from the server. This can be achieved by having the PHP script write log messages to a file or database, and then using JavaScript to make asynchronous requests to fetch and display these logs in real-time on the web interface.

<?php
// PHP script to write log messages to a file
$logFile = 'log.txt';

// Write log message to file
function writeLog($message) {
    file_put_contents($logFile, $message . PHP_EOL, FILE_APPEND);
}

// Example usage
writeLog('Log message 1');
writeLog('Log message 2');
?>