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');
?>
Related Questions
- Is it best practice to store language selection in a session in PHP applications?
- What are the potential pitfalls of using "SELECT *" in SQL queries, and what are some best practices for avoiding them?
- What are the advantages and disadvantages of using session variables in PHP for form data retention?