What is the Session library in CodeIgniter 4?
Why Interviewers Ask This
This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex CodeIgniter topics. It also reveals how well you can explain technical ideas to non-experts.
Answer
CodeIgniter 4 provides a robust Session library supporting multiple drivers: files (default), database, redis, and memcached. Configure in app/Config/Session.php. Get the session service: $session = service("session") or use the global session() function. Set data: $session->set("user_id", 1) or $session->set(["user_id" => 1, "role" => "admin"]). Get data: $session->get("user_id"). Check: $session->has("user_id"). Remove: $session->remove("user_id"). Flashdata (available only for the next request): $session->setFlashdata("msg", "Saved!"), $session->getFlashdata("msg"). Tempdata (flashdata with custom expiry in seconds): $session->setTempdata("item", "value", 300).
Pro Tip
Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex CodeIgniter answers easy to follow.