Where to set session in Codeigniter?

Where to set session in Codeigniter?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Where to Set Sessions in CodeIgniter

In CodeIgniter, the architecture dictates a specific flow for handling user state: you load the session library in your controller, set data there immediately after successful authentication, and then check that existence in subsequent controllers or views. The confusion often stems from trying to manage this logic inside the login view itself or misunderstanding where the session helper lives versus the Session object.

When you are in a file like login.php, which typically acts as a view rendering HTML forms, you should not be setting sessions directly there. Views are passive; they display data but do not hold application state logic. If your login.php is actually a controller method (common in older tutorials where naming conventions were looser), then yes, that is the correct place to validate credentials and write the session data.

The standard pattern involves loading the Session library at the top of the controller action:

$this->load->library('session');

Once the library is loaded, you can store user information. If your validation passes, you might do something like this:

$data = array(
    'username' => $this->input->post('username'),
    'email'    => $data['username'] // Example logic
);

$this->session->set_userdata($data);
// Or for a specific key:
$this->session->set_userdata('user_data', $data);

Notice the distinction between set_userdata() and unset_userdata(). You are adding state to the session so that subsequent requests can access it without re-running the login logic.

Checking Login Status in Other Pages

Now, consider your admin.php controller. How do you verify if a user is logged in before allowing them to view sensitive data? You don't check for a variable named "session exists" directly; instead, you inspect the specific key you stored during authentication.

The method $this->session->user_data('item') is not standard CodeIgniter syntax. The correct approach is to retrieve the value associated with your session key. If you stored data under the key 'user_data', you would check it like this:

$user = $this->session->userdata('user_data');
if ($user) {
    // User is logged in
} else {
    // Redirect to login or show error
}

Alternatively, if you stored a boolean flag specifically for authentication status, such as 'logged_in', the check becomes simpler:

if ($this->session->userdata('logged_in')) {
    $this->load->view('admin/dashboard');
} else {
    redirect('/login', 'location');
}

Handling Session Destruction and Security

A critical trade-off in this architecture is session persistence. When a user logs out, simply clearing the database entry isn't enough; you must explicitly destroy the session data to prevent unauthorized access if the user closes their browser unexpectedly or if the session cookie remains valid on a different device.

To securely log out, you should use the sess_destroy method:

$this->session->sess_destroy();
// Optionally clear the database record here as well

This ensures that even if the session ID is intercepted or reused, the application considers the user logged out because the stored credentials are gone.

Another common pitfall involves mixing helper functions with library objects. In older CodeIgniter versions (like 2.x), you might see references to $this->session being used as a helper ($this->session->userdata). While session() is a global function available via the helper, using $this->session implies the library is loaded. Always ensure load->library('session') has been called before accessing $this->session->userdata().

If you are migrating from CodeIgniter 2 to modern frameworks like Laravel, this pattern of manual session handling in controllers shifts toward middleware and request filters. However, understanding this specific flow in CodeIgniter is essential for maintaining legacy applications or building custom solutions where you need granular control over session keys without relying on the framework's default guard objects.

For developers looking to explore how modern frameworks abstract these concepts further, resources like https://laravelcompany.com often provide useful comparisons on state management patterns across different PHP ecosystems.

Our network