Files
sistema_funcionando_lastwar/includes/activity_logger.php

35 lines
1.1 KiB
PHP
Executable File

<?php
require_once __DIR__ . '/db.php';
function log_activity(int $userId, string $action, ?string $details = null): void
{
global $pdo;
// Debugging: Check if $pdo is a valid PDO object
if (!($pdo instanceof PDO)) {
error_log("Error logging activity: $pdo is not a valid PDO object. Type: " . gettype($pdo));
return;
}
try {
// Fetch username from session or database if not available
$username = $_SESSION['username'] ?? 'Unknown';
if ($username === 'Unknown' && $userId > 0) {
$stmt = $pdo->prepare("SELECT username FROM users WHERE id = ?");
$stmt->execute([$userId]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user) {
$username = $user['username'];
}
}
$stmt = $pdo->prepare(
"INSERT INTO activity_log (user_id, username, action, details, timestamp)
VALUES (?, ?, ?, ?, NOW())"
);
$stmt->execute([$userId, $username, $action, $details]);
} catch (PDOException $e) {
error_log("Error logging activity: " . $e->getMessage());
}
}
?>