<?php
// Start session
session_start();

// Check if user is logged in
function isLoggedIn() {
    return isset($_SESSION['user_id']) && !empty($_SESSION['user_id']);
}

// Redirect to login if not authenticated
function requireLogin() {
    if (!isLoggedIn()) {
        header('Location: login.php');
        exit();
    }
}

// Login user
function loginUser($userId, $userName) {
    $_SESSION['user_id'] = $userId;
    $_SESSION['user_name'] = $userName;
}

// Logout user
function logoutUser() {
    session_destroy();
    header('Location: login.php');
    exit();
}

// Get current user info
function getCurrentUser() {
    if (isLoggedIn()) {
        return [
            'id' => $_SESSION['user_id'],
            'name' => $_SESSION['user_name']
        ];
    }
    return null;
}
?>
