What best practices should be followed when logging user activity in a PHP application?
When logging user activity in a PHP application, it is important to follow best practices to ensure security and efficiency. Some key practices include sanitizing user input to prevent SQL injection attacks, using a secure logging mechanism to store sensitive data, and implementing proper error handling to catch any issues that may arise during logging.
// Example of logging user activity in a PHP application
// Sanitize user input
$user_id = filter_var($_POST['user_id'], FILTER_SANITIZE_NUMBER_INT);
$action = filter_var($_POST['action'], FILTER_SANITIZE_STRING);
// Log user activity
$log_message = "User with ID $user_id performed action: $action";
file_put_contents('user_activity.log', $log_message . PHP_EOL, FILE_APPEND);