How They Work Together
How They Work Together
text
| Function | Role |
|---------------------------|-------------------------------------------------------------------|
| `startUserSession` | Called on login — creates a DB row, returns `session_id`. |
| `getSessionDuration` | Can be called anytime to report how long the session has lasted. |
| `hasActiveUserSession` | Used to enforce single-session-per-user or detect stale sessions. |
| `endUserSession` | Called on logout or timeout — closes the DB row. |
| `checkSessionTimeout` | Called on each request — idle check + auto-logout. |
- User logs in →
startUserSession($pdo)→ store returned ID in$_SESSION['session_id']. - On each page load →
checkSessionTimeout()runs. - If the user is idle too long →
endUserSession($pdo)+ destroy session + redirect. - On manual logout →
endUserSession($pdo)+ destroy session. getSessionDuration($pdo)andhasActiveUserSession($pdo, $user_id)are utility/query functions used where needed.
---
checkSessionTimeoutusesglobal $pdo— passing$pdoas a parameter (like the other functions) would be cleaner and more consistent.startUserSessiondoesn't store the returned ID in$_SESSION['session_id']itself — the caller must do it. Otherwise subsequent functions (endUserSession,getSessionDuration) won't work.session_destroy()doesn't unset$_SESSIONin all cases;session_unset()is called first here, which is correct.BASE_URLmust be defined elsewhere (e.g. a config file) for the redirect to work.
Minor Notes / Potential Improvements
Sign in to save your progress permanently.
Progress is saved in your browser session
Step 8 of 8