Login System Overview
What is a Login System?
Section titled โWhat is a Login System?โA login system is a security mechanism that verifies user identity (authentication), controls access to resources (authorization), maintains user sessions across requests, and protects sensitive information.
Think of it like a building with a security desk: registration is like getting an ID card, logging in is showing your ID to enter, the session is wearing a visitor badge while inside, and logging out is returning your badge and leaving.
Core Components
Section titled โCore ComponentsโA complete login system has three main components:
- User Registration: creating new user accounts, collecting user information, and storing passwords securely.
- User Login: verifying user credentials, creating authenticated sessions, and managing user state.
- User Logout: destroying sessions, clearing user state, and performing security cleanup.
Each of these is covered in detail on its own page.
Authentication vs Authorization
Section titled โAuthentication vs AuthorizationโThese two concepts are often confused but serve different purposes.
Authentication answers the question โWho are you?โ It involves verifying user identity by checking credentials (email + password) and creating sessions after a successful login.
Authorization answers the question โWhat can you do?โ It involves checking user permissions, enforcing role-based access control (e.g., Admin vs Customer), and protecting specific resources.
For example, a user logs in with their email and password (authentication). Once logged in, an admin user can access /admin/dashboard, but a regular customer cannot (authorization).
Why Sessions Matter
Section titled โWhy Sessions MatterโHTTP is a stateless protocol. Without sessions, the server would forget who the user is after every single request:
Without sessions: Request 1: User logs in (OK) Request 2: User visits /dashboard (Who are you? Login again!) Request 3: User visits /profile (Who are you? Login again!)
With sessions: Request 1: User logs in (Session created) Request 2: User visits /dashboard (Session remembers user) Request 3: User visits /profile (Session remembers user)Sessions solve this by storing user data on the server and identifying the user through a session ID cookie sent with each request.
File Structure
Section titled โFile StructureโHere is the MVC organization you will build across the authentication labs:
app/โโโ Controllers/โ โโโ AuthController.php (register, login, logout, dashboard)โโโโ Domain/Models/โ โโโ UserModel.php (create, find, authenticate)โโโโ Middleware/โ โโโ SessionMiddleware.php (global session management)โ โโโ AuthMiddleware.php (login check)โ โโโ AdminAuthMiddleware.php (role check)โโโโ Views/โ โโโ auth/ (register.php, login.php)โ โโโ user/ (dashboard.php)โโโโ Routes/ โโโ web-routes.php (route definitions)