What is the best way to implement a logging function in PHP for tracking user activity on a portal?
To implement a logging function in PHP for tracking user activity on a portal, you can create a function that writes relevant information to a log file whenever a user performs an action. This can include logging details such as the user's IP address, the action taken, and the timestamp. By including this logging function in key areas of your portal, you can easily track user activity and troubleshoot any issues that may arise.
function logUserActivity($userId, $action) {
$logFile = 'user_activity.log';
$timestamp = date('Y-m-d H:i:s');
$ipAddress = $_SERVER['REMOTE_ADDR'];
$logMessage = "$timestamp - User $userId from IP $ipAddress performed action: $action\n";
file_put_contents($logFile, $logMessage, FILE_APPEND);
}
// Example usage:
logUserActivity(123, 'Logged in');
Keywords
Related Questions
- What are the best practices for updating Bootstrap versions in PHP projects to avoid compatibility issues?
- How can PHP developers handle situations where certain columns like 'Instrument' are only relevant for specific categories like 'Soloists' in a unified table structure for 'Musicians'?
- What potential issues can arise when upgrading PHP versions on a Raspberry Pi?