Spaces:
Sleeping
Sleeping
| /** | |
| * Authentication Check | |
| * | |
| * This file checks if a user is logged in and redirects if necessary. | |
| * It also provides functions for authentication-related operations. | |
| */ | |
| // Start session if not already started | |
| if (session_status() === PHP_SESSION_NONE) { | |
| session_start(); | |
| } | |
| /** | |
| * Check if user is logged in | |
| * | |
| * @return boolean True if user is logged in, false otherwise | |
| */ | |
| function isLoggedIn() { | |
| return isset($_SESSION['user_id']) && !empty($_SESSION['user_id']); | |
| } | |
| /** | |
| * Check if current user is a faculty member | |
| * | |
| * @return boolean True if user is faculty, false otherwise | |
| */ | |
| function isFaculty() { | |
| return isLoggedIn() && $_SESSION['user_type'] === 'faculty'; | |
| } | |
| /** | |
| * Check if current user is a student | |
| * | |
| * @return boolean True if user is a student, false otherwise | |
| */ | |
| function isStudent() { | |
| return isLoggedIn() && $_SESSION['user_type'] === 'student'; | |
| } | |
| /** | |
| * Redirect non-logged in users to login page | |
| * | |
| * @param string $message Optional message to display after redirect | |
| * @return void | |
| */ | |
| function requireLogin($message = 'Please login to access this page.') { | |
| if (!isLoggedIn()) { | |
| $_SESSION['auth_message'] = $message; | |
| header('Location: login.php'); | |
| exit; | |
| } | |
| } | |
| /** | |
| * Redirect non-faculty users | |
| * | |
| * @param string $message Optional message to display after redirect | |
| * @return void | |
| */ | |
| function requireFaculty($message = 'Faculty access required.') { | |
| requireLogin(); | |
| if (!isFaculty()) { | |
| $_SESSION['auth_message'] = $message; | |
| header('Location: index.php'); | |
| exit; | |
| } | |
| } | |
| /** | |
| * Require user to be student to access page | |
| * Redirects to index page if not student | |
| */ | |
| function requireStudent() { | |
| requireLogin(); | |
| if (!isStudent()) { | |
| header("Location: index.php"); | |
| exit; | |
| } | |
| } | |
| /** | |
| * Get user ID from session | |
| * | |
| * @return mixed User ID if logged in, null otherwise | |
| */ | |
| function getCurrentUserId() { | |
| return isLoggedIn() ? $_SESSION['user_id'] : null; | |
| } | |
| /** | |
| * Get username from session | |
| * | |
| * @return mixed Username if logged in, null otherwise | |
| */ | |
| function getCurrentUsername() { | |
| return isLoggedIn() ? $_SESSION['username'] : null; | |
| } | |
| /** | |
| * Get user type from session | |
| * | |
| * @return mixed User type if logged in, null otherwise | |
| */ | |
| function getCurrentUserType() { | |
| return isLoggedIn() ? $_SESSION['user_type'] : null; | |
| } | |
| /** | |
| * Update the last login time for a user | |
| */ | |
| function updateLastLogin($conn, $user_id) { | |
| $query = "UPDATE users SET last_login = NOW() WHERE id = ?"; | |
| $stmt = $conn->prepare($query); | |
| $stmt->bind_param("i", $user_id); | |
| $stmt->execute(); | |
| } | |
| // Define variables to use in header | |
| $is_logged_in = isLoggedIn(); | |
| $is_faculty = isFaculty(); | |
| $is_student = isStudent(); | |