Code Igniter User Guide Version 1.4.0


Cookie Helper

The Cookie Helper file contains functions that assist in working with cookies.

Loading this Helper

This helper is loaded using the following code:

$this->load->helper('cookie');

The following functions are available:

set_cookie()

Sets a cookie containing the values you specify. There are two ways to pass information this function so that a cookie can be set: Arrray Method, and Discreet Parameters:

Array Method

Using this method, an associative array is passed to the first parameter:

$cookie = array(
                   'name'   => 'The Cookie Name',
                   'value'  => 'The Value',
                   'expire' => '86500',
                   'domain' => '.some-domain.com',
                   'path'   => '/',
                   'prefix' => 'myprefix_',
               );

set_cookie($cookie);

Notes:

Only the name and value are required.

The expiration is set in seconds, which will be added to the current time. Do not include the time, but rather only the number of seconds from now that you wish the cookie to be valid. If the expiration is set to zero the cookie will only last as long as the browser is open.

To delete a cookie set it with the expiration blank.

For site-wide cookies regardless of how your site is requested, add your URL to the domain starting with a period, like this: .your-domain.com

The path is usually not needed since the function sets a root path.

The prefix is only needed if you need to avoid name collisions with other identically named cookies for your server.

Discreet Parameters

If you prefer, you can set the cookie by passing data using individual parameters:

set_cookie($name, $value, $expire, $domain, $path, $prefix);