Spaces:
Running
Running
File size: 2,339 Bytes
8788b3a | 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 | ```php
<?php
session_start();
// Database connection
$host = 'localhost';
$dbname = 'uatc_usis';
$username = 'root';
$password = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
$role = $_POST['role'] ?? '';
// Validate input
if (empty($username) || empty($password) || empty($role)) {
header('Location: login.html?error=missing_fields');
exit();
}
// Query based on role
$table = '';
switch ($role) {
case 'student':
$table = 'students';
break;
case 'lecturer':
$table = 'lecturers';
break;
case 'admin':
$table = 'administrators';
break;
default:
header('Location: login.html?error=invalid_role');
exit();
}
try {
$stmt = $pdo->prepare("SELECT * FROM $table WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user && password_verify($password, $user['password'])) {
// Set session variables
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['role'] = $role;
$_SESSION['full_name'] = $user['full_name'];
// Redirect based on role
switch ($role) {
case 'student':
header('Location: student_dashboard.html');
break;
case 'lecturer':
header('Location: lecturer_dashboard.html');
break;
case 'admin':
header('Location: index.html');
break;
}
exit();
} else {
header('Location: login.html?error=invalid_credentials');
exit();
}
} catch (PDOException $e) {
header('Location: login.html?error=database_error');
exit();
}
} else {
header('Location: login.html');
exit();
}
?>
``` |