🎯 Features:
- Login system
- Form validation
- Role-based dashboard
- Status using ternary
- Match (modern PHP)
📁 1. index.php (Login Form)
<form method="POST" action="login.php">
<h2>Login</h2>
<input type="text" name="username" placeholder="Enter Username"><br><br>
<input type="password" name="password" placeholder="Enter Password"><br><br>
<button type="submit">Login</button>
</form>
🔐 2. login.php (Validation + Login Logic)
<?php
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';// ✅ Form Validation (if-else)
if (empty($username) || empty($password)) {
echo "All fields are required ❗";
exit;
}// ✅ Dummy Users (Database simulation)
$users = [
"admin" => ["password" => "1234", "role" => "admin"],
"john" => ["password" => "1111", "role" => "user"],
"editor"=> ["password" => "2222", "role" => "editor"]
];// ✅ Login Check
if (isset($users[$username]) && $users[$username]['password'] === $password) {
$role = $users[$username]['role']; // ✅ Ternary (status message)
$status = ($role === "admin") ? "High Privilege User" : "Normal User"; echo "Login Successful ✅<br>";
echo "Status: $status <br>"; // Redirect to dashboard
header("refresh:2;url=dashboard.php?role=$role");} else {
echo "Invalid Credentials ❌";
}
?>
🧠 3. dashboard.php (Switch + Match)
<?php
$role = $_GET['role'] ?? 'guest';echo "<h2>Dashboard</h2>";// ✅ Switch Case (old method)
switch ($role) {
case "admin":
echo "Welcome Admin 👑<br>";
break;
case "editor":
echo "Welcome Editor ✍️<br>";
break;
case "user":
echo "Welcome User 👤<br>";
break;
default:
echo "Welcome Guest 🚫<br>";
}// ✅ Match Expression (modern PHP 8+)
$access = match($role) {
"admin" => "You have FULL access",
"editor" => "You can EDIT content",
"user" => "You can VIEW content",
default => "No Access",
};echo "<br>Access Level: $access";
?>
🧪 What Students Learn
✔ If-Else → form validation & login
✔ Ternary → quick status logic
✔ Switch → role-based UI
✔ Match → modern cleaner logic
✔ Real-world structure (multi-file project)
💡 Extra Improvements (Homework)
Ask students to:
- Add logout button
- Use sessions instead of GET
- Add registration page
- Store users in database (MySQL)
🎯 Teaching Tip
Run this project locally (XAMPP/WAMP):
http://localhost/project-folder/






