<?php
session_start();
/**
* Check if a user is currently logged in.
*
* @return bool True if a user session exists, false otherwise.
*/
function is_logged_in() {
return isset($_SESSION['user']);
}
/**
* Redirect to the login page if the user is not logged in.
*
* This function should be called at the top of any page that requires authentication.
* If the user is not logged in, they will be redirected and script execution will stop.
*
* @return void
*/
function redirect_if_not_logged_in() {
if (!is_logged_in()) {
header('Location: ../index.php?page=login');
exit;
}
}
/**
* Get the current logged-in user's ID.
*
* @return int|null The user's primary key (pk_user) if logged in, or null if not.
*/
function get_current_user_id() {
return $_SESSION['user']['pk_user'] ?? null;
}
?>