PHP 7 interview question · Most asked
How do PHP sessions work under the hood?
Summary
session_start() reads a session ID from a cookie (default PHPSESSID), loads serialized data from the save handler (files by default) into $_SESSION, and writes it back at request end, holding a lock in between.
Longer Answer
Default file sessions live in session.save_path, and garbage collection is probabilistic (gc_probability/gc_divisor, gc_maxlifetime). The file lock serializes concurrent requests from the same user. Harden with use_strict_mode, cookie_secure, cookie_httponly and cookie_samesite, and regenerate IDs on login.
Key Points
- Cookie ID → server-side data
- File locks serialize concurrent requests
- Probabilistic GC
- Harden cookie flags, regenerate on login
More most-asked PHP 7 questions
- What are typed properties in PHP 7.4?
- How do return type declarations work in PHP 7?
- What were the headline improvements of PHP 7.0 over PHP 5.6?
- Explain scalar type declarations and coercive vs strict mode in PHP 7.
- Explain the `Throwable`, `Error` and `Exception` hierarchy introduced in PHP 7.
- What are nullable types in PHP 7.1?
- What does the null coalescing operator `??` do, and how is it different from `isset()` with a ternary?
- What are arrow functions in PHP 7.4 and how do they differ from closures?