Spaces:
Running
Running
Download login_process.php from danielmkinga92/usis: direct link, hf CLI and curl.
- Browser
- Download file 2.34 kB
-
https://huggingface.co/spaces/danielmkinga92/usis/resolve/main/login_process.php
- Command line
-
hf download hf://spaces/danielmkinga92/usis/login_process.php
-
curl -L -o login_process.php https://huggingface.co/spaces/danielmkinga92/usis/resolve/main/login_process.php
2.34 kB
| ```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(); | |
| } | |
| ``` |