What are some best practices for securely accessing and manipulating log files using PHP on a web server?

It is important to securely access and manipulate log files in PHP on a web server to prevent unauthorized access or malicious activities. One best practice is to ensure that log files are stored in a directory with proper permissions set to restrict access. Additionally, it is recommended to sanitize user input before using it to access or manipulate log files to prevent injection attacks.

<?php

// Ensure that log files are stored in a secure directory with restricted access
$logFilePath = '/path/to/log/file.log';

// Sanitize user input before using it to access or manipulate log files
$userInput = $_POST['user_input'];
$sanitizedInput = filter_var($userInput, FILTER_SANITIZE_STRING);

// Example of securely accessing log file
if (file_exists($logFilePath)) {
    $logFileContent = file_get_contents($logFilePath);
    echo $logFileContent;
} else {
    echo 'Log file not found';
}

?>