2026-01-23 20:42:30 +00:00

36 lines
746 B
PHP

<?php
declare(strict_types=1);
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
function isLoggedIn(): bool {
return isset($_SESSION['user_id']);
}
function requireLogin(): void {
if (!isLoggedIn()) {
header('Location: login.php');
exit;
}
}
function getCurrentUser(): ?array {
if (!isLoggedIn()) return null;
static $user;
if (!$user) {
$stmt = db()->prepare("SELECT u.*, c.name as company_name FROM users u JOIN companies c ON u.company_id = c.id WHERE u.id = ?");
$stmt->execute([$_SESSION['user_id']]);
$user = $stmt->fetch();
}
return $user;
}
function logout(): void {
session_destroy();
header('Location: login.php');
exit;
}