40097-vm/sesiones_iniciadas.php
2026-05-12 10:55:49 +00:00

86 lines
3.1 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
require_once 'db/config.php';
$user_role = $_SESSION['user_role'];
if ($user_role !== 'Administrador' && $user_role !== 'admin') {
header('Location: dashboard.php');
exit;
}
$pdo = db();
$pageTitle = "Sesiones Iniciadas";
// Obtener el historial de sesiones
$stmt = $pdo->query("
SELECT s.*, u.username, u.nombre_asesor
FROM user_sessions s
JOIN users u ON s.user_id = u.id
ORDER BY s.login_time DESC
LIMIT 500
");
$sessions = $stmt->fetchAll(PDO::FETCH_ASSOC);
include 'layout_header.php';
?>
<div class="container-fluid">
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Historial de Inicios de Sesión (Últimos 500)</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="sessionsTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Usuario</th>
<th>Nombre/Asesor</th>
<th>Fecha y Hora</th>
<th>Dirección IP</th>
<th>Ciudad</th>
<th>Navegador / Dispositivo</th>
</tr>
</thead>
<tbody>
<?php foreach ($sessions as $session): ?>
<tr>
<td><?php echo htmlspecialchars($session['username']); ?></td>
<td><?php echo htmlspecialchars($session['nombre_asesor'] ?? 'N/A'); ?></td>
<td><?php echo date('d/m/Y H:i:s', strtotime($session['login_time'])); ?></td>
<td><?php echo htmlspecialchars($session['ip_address']); ?></td>
<td><span class="badge bg-info text-dark"><?php echo htmlspecialchars($session['ciudad'] ?? 'N/A'); ?></span></td>
<td style="font-size: 0.85rem;"><?php echo htmlspecialchars($session['user_agent']); ?></td>
</tr>
<?php endforeach; ?>
<?php if (empty($sessions)): ?>
<tr>
<td colspan="6" class="text-center">No hay registros de sesiones aún.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
<script>
$(document).ready(function() {
$('#sessionsTable').DataTable({
"order": [[ 2, "desc" ]],
"language": {
"url": "//cdn.datatables.net/plug-ins/1.13.6/i18n/es-ES.json"
}
});
});
</script>
<?php include 'layout_footer.php'; ?>