Spaces:
Sleeping
Sleeping
File size: 2,895 Bytes
102fe5c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | <?php
/**
* 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();
?> |