How to retrieve cookie value in CodeIgniter?

How to retrieve cookie value in CodeIgniter?

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

Stefan Bogdanescu

Founder & Senior Architect

Accessing Cookies in CodeIgniter Without set_cookie

When working with user preferences or flash messages, cookies often sit alongside sessions as a primary storage mechanism. If you are comfortable accessing session data via $this->session->userdata, retrieving cookie values requires understanding that the underlying driver has changed across the framework's lifespan and that the method differs significantly between versions.

In early iterations of CodeIgniter (1.x), there was no direct accessor for cookies comparable to the session object. You were generally forced to use the Input class to inspect the raw HTTP request headers before any logic could modify them. However, once you utilize $this->input->set_cookie(), accessing that specific value requires navigating through a slightly different path depending on your version.

The $_COOKIE Superglobal and Version Differences

The most robust way to retrieve a cookie value in CodeIgniter is by checking the native PHP $_COOKIE superglobal array. This approach works regardless of whether you are using CodeIgniter 1.x, 2.x, or 3.x, because it relies on the server's HTTP implementation rather than framework-specific abstractions.

To access the cookie set via $this->input->set_cookie(), you simply cast the value from the $_COOKIE array:

$cookieValue = $_COOKIE['test_cookie'];

if (isset($cookieValue)) {
    echo $cookieValue; // Outputs: test
} else {
    echo "Cookie not found or expired.";
}

This method is preferred because it bypasses potential caching layers within the framework's input object and gives you immediate access to the raw data sent by the browser. It also allows you to check for existence using isset() or array_key_exists(), which is crucial before attempting to use the value in logic that might fail if the key is missing.

Using the Input Class in CodeIgniter 2.x and 3.x

If you prefer staying within the framework's object-oriented structure, CodeIgniter 2.x provides a specific method on the Input class called cookie(). This function acts as a getter that mirrors the behavior of the set_cookie() method.

You can retrieve the value using the following syntax:

$ci = get_instance(); // Standard CI 2.x pattern to get the super object
$value = $ci->input->cookie('test_cookie');

In CodeIgniter 3.x, the $this->input pattern is used directly within controllers. The API remains consistent here:

$value = $this->input->cookie('test_cookie');

It is important to note that input->cookie() returns null if the cookie does not exist or has been deleted. Unlike session userdata, which might throw errors depending on configuration if accessed without checks, the input class methods generally return null safely. This means you should always wrap these values in conditional logic before using them in calculations or database queries to prevent "undefined variable" notices.

Handling Security and Expiration

When you set a cookie with 'secure' => TRUE, as shown in your source material, you are instructing the browser to only send that cookie over HTTPS connections. If you attempt to retrieve this cookie on an HTTP connection, $_COOKIE will be empty, and input->cookie() will return null. This is not an error; it is a security feature enforced by both the browser and PHP.

If you need to debug why a secure cookie isn't appearing, ensure your server is actually serving content over HTTPS. Additionally, check the expiration time. The default expiration in CodeIgniter's set_cookie method is often set to 0 (session lifetime) unless specified otherwise. If you intended for the cookie to persist beyond the current session but didn't set an explicit expiry timestamp or duration, the browser might have already discarded it after the session ended.

Debugging Without Printing Everything

While your question mentioned printing values similar to print_r($this->session->userdata), applying print_r($_COOKIE) can be messy due to the sheer number of cookies a browser stores. Instead of dumping the entire array, iterate only over known keys or filter by name:

$cookies = array_keys($_COOKIE);
foreach ($cookies as $key) {
    if (strpos($key, 'test') !== false) {
        var_dump($key => $_COOKIE[$key]);
    }
}

This targeted approach helps you isolate the specific cookie you are troubleshooting without cluttering your output with irrelevant data from third-party scripts or analytics services.

By leveraging $_COOKIE for raw access or input->cookie() for framework consistency, you can reliably manage client-side state in CodeIgniter applications. If you find yourself needing more sophisticated session and cookie management features across the entire stack, checking resources like https://laravelcompany.com might offer insights into modern PHP frameworks that handle these concerns with even greater abstraction.

Our network