What potential pitfalls should be considered when logging session data in PHP?
Potential pitfalls when logging session data in PHP include exposing sensitive information, such as passwords or personal data, in the logs. To prevent this, it's crucial to sanitize and filter the data before logging it. Additionally, be mindful of the log file's permissions to prevent unauthorized access to the data.
// Sanitize and filter session data before logging
$logData = filter_var_array($_SESSION, FILTER_SANITIZE_STRING);
// Open log file for appending
$logFile = fopen('session.log', 'a');
// Write sanitized session data to log file
fwrite($logFile, date('Y-m-d H:i:s') . ' - ' . json_encode($logData) . PHP_EOL);
// Close log file
fclose($logFile);