Overview
User sessions allow a web application to remember that a user has authenticated while they navigate between pages. Instead of requiring the user to log in on every page, the server creates a session and associates it with the user's browser.
1. Session Flow
text
┌──────────────┐
│ User visits │
│ Login Page │
└──────┬───────┘
│
▼
┌──────────────────┐
│ Submit username │
│ and password │
└──────┬───────────┘
│
▼
┌─────────────────────────┐
│ Server verifies account │
│ using database │
└──────┬──────────────────┘
│
▼
┌─────────────────────────┐
│ Create authenticated │
│ session │
│ │
│ $_SESSION['user_id'] │
│ $_SESSION['user_role'] │
└──────┬──────────────────┘
│
▼
┌──────────────────┐
│ Redirect to │
│ Dashboard │
└──────┬───────────┘
│
▼
┌─────────────────────────┐
│ Every protected page │
│ checks the session │
└──────┬──────────────────┘
│
├── Authenticated ──► Allow access
│
└── Not authenticated
│
▼
Login Page
2. What Is a Session?
A session is server-side state associated with a particular browser. After successful login, PHP can store:
php
$_SESSION['user_id'] = 25;
$_SESSION['user_role'] = 'manager';
$_SESSION['user_email'] = 'user@example.com';
The browser should contain only the session identifier, while sensitive session data remains on the server.
Sign in to save your progress permanently.
Progress is saved in your browser session
Step 1 of 8