📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials CodeIgniter 4 Sessions in CI4

Sessions in CI4

5 min read Quiz at the end
Manage session data with set, get, flash, tempdata, regenerate, and destroy operations.

Sessions in CI4

// app/Config/App.php
// Session driver: files, database, redis, memcached
// app/Config/Session.php
$session = ConfigServices::session();

// Or use global function
session()->set("user_id", 42);
session()->set(["user_id" => 42, "role" => "admin"]);
session()->get("user_id");
session()->has("user_id");  // true/false
session()->remove("user_id");
session()->destroy();
session()->regenerate(true);  // prevent fixation

// Flash data (survives one redirect)
session()->setFlashdata("success", "Saved!");
session()->getFlashdata("success");

// Temp data (with TTL)
session()->setTempdata("otp", "123456", 300); // 5 min

// In views
<?php if ($success = session("success")): ?>
    <div class="alert"><?= esc($success) ?></div>
<?php endif ?>
Topic Quiz · 2 questions

Test your understanding before moving on

1. How do you store flash data in CI4?
💡 setFlashdata() stores data automatically deleted after the next request.
2. What does session()->regenerate(true) do?
💡 regenerate(true) creates new session ID while deleting old one for security.