codeigniter check for user session in every controller

codeigniter check for user session in every controller

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

Stefan Bogdanescu

Founder & Senior Architect

Decoupling Session Logic from Controllers in CodeIgniter

You've hit a classic architectural trap: duplicating business logic or utility functions across multiple controllers to avoid violating the DRY (Don't Repeat Yourself) principle. In your specific case, you have a private method _is_logged_in() inside one controller that checks for user_data in the session. While this works locally, it doesn't scale when you need that same check in every other controller in the application.

Redefining this function in every controller is indeed an anti-pattern. It creates maintenance overhead where a single line of code change requires updates in dozens of files. It also couples your authentication logic tightly to specific controller classes, making the code harder to test and refactor.

To solve this, you need to move the logic out of the controller hierarchy entirely. In CodeIgniter, there are three primary architectural patterns to handle this, ranging from simple utility functions to robust design patterns.

The Utility Class Approach

The most straightforward solution is to extract the method into a dedicated Auth or Session utility class. You can place this file in the application/helpers directory (if you want to autoload it globally) or simply reference the class directly where needed.

Create a new file at application/libraries/Auth.php:

class Auth {
    public function is_logged_in() {
        $session = &get_instance(); // Access the superobject
        $user_data = $session->userdata('user_data');
        
        return isset($user_data) ? true : false;
    }
    
    // You can add logout logic here too for consistency
    public function logout() {
        $session = &get_instance();
        $session->sess_destroy();
    }
}

In your controllers, you now access this via $this->auth (if configured in autoload.php) or by instantiating the class manually. This keeps your controllers clean and focused solely on handling HTTP requests and responses.

The Front Controller Pattern

If you are building a larger application where every controller needs to verify session state before processing any logic, relying on manual checks inside every controller method is still inefficient. Instead, intercept the request at the entry point.

In CodeIgniter, the index.php file loads the framework and triggers the bootstrap process. You can wrap your application logic in a custom Front Controller or modify the existing one to load an Application.php class that acts as a global controller.

Inside this central controller, you can define a method like _check_session() that runs once per request. If the session isn't valid, you can immediately redirect to a login page using $this->load->view('login') or $this->redirect('/login'). This way, no individual controller needs to know about the session check; it happens automatically before any controller action executes.

Using Filters (Hooks)

CodeIgniter also supports filters via Hooks. You can configure a hook that triggers on every controller call (pre_controller) to validate the session. However, hooks are generally considered less explicit than classes or front controllers. They make the flow of execution slightly more opaque and harder to trace during debugging. If you choose this route, ensure your hook configuration in application/hooks.php clearly documents which action it intercepts.

Trade-offs and Best Practices

When deciding between these approaches, consider the complexity of your application. A simple utility class is low-overhead and easy to understand. It works well if you only need to check the session occasionally. However, if authentication failure should halt all non-authenticated requests regardless of which controller handles them, the Front Controller pattern provides a stronger guarantee.

Be careful with how you access the session object. In older CodeIgniter versions, $this->session is available directly in controllers and models. In newer versions or specific configurations, you might need to use the superobject &get_instance() or configure the Session library as an autoloader to ensure consistent access across your entire stack.

Finally, remember that session data can be volatile. Always handle cases where the session has expired or been cleared on the client side versus a server-side reset. Your _is_logged_in check should ideally include logic to detect if the user was redirected without a valid token, not just whether the key exists in the array.

For projects transitioning from legacy frameworks like CodeIgniter to modern PHP stacks, you might find that switching to Laravel simplifies these concerns significantly. The Authenticatable trait and built-in middleware in https://laravelcompany.com handle session validation automatically, abstracting away the need for manual checks or front controller logic entirely.

Our network