Cookie Class

The cookie class allows you to get, set and delete cookies.

Configuration

The cookie class is configured through the global application configuration file, app/config/config.php. It defines a section called 'cookie', in which the following settings are defined:

Variable Type Default Description
expiration integer
0
Number of seconds before the cookie expires. This value will be used when $expiration is not specified when you call the set() method.
path string
'/'
Restrict the path that the cookie is available to. This value will be used when $path is not specified when you call the set() method.
domain string
null
Restrict the domain that the cookie is available to. This value will be used when $domain is not specified when you call the set() method.
secure boolean
false
Set to true if you only want to transmit cookies over secure connections.
httponly boolean
false
Allow only transmit of cookies over HTTP, disabling Javascript access.

If one or more of these values are missing from the global configuration, the class will use the defaults as defined in this table.

When using cookies, make ABSOLUTELY sure that the timezone configured in your app/config/config.php and/or your php.ini file matches the timezone set on your server. Since cookie expiry timestamps are in GMT, expiry time calculation goes horribly wrong when you have a timezone mismatch, ranging from not expiring properly to cookies not set at all because they arrive at the browser already expired.

get($name = null, $default = null)

The get method allows you to read a $_COOKIE variable. If no name given, all cookies are returned.

Static Yes
Parameters
Param Default Description
$name Optional The key in the $_COOKIE array.
$default
null
What value should be returned if the array item is not found?
Returns mixed
Example
$theme = Cookie::get('theme', 'blue');

set($name, $value, $expiration = null, $path = null, $domain = null, $secure = null, $httponly = null)

The set method allows you to create a cookie.

Static Yes
Parameters
Param Default Description
$name Required The key in the $_COOKIE array.
$value Required The value of the cookie.
$expiration
null
Number of seconds the cookie should last for.
$path
null
The path on the server in which the cookie will be available on.
$domain
null
The domain that the cookie is available to.
$secure
null
Set to true if you only want to transmit cookies over secure connections.
$httponly
null
Allow only transmit of cookies over HTTP, disabling Javascript access.
Returns boolean
Example
Cookie::set('theme', 'blue', 60 * 60 * 24);

For every parameter not specified or defined as null, the globally defined configuration value will be substituted.

delete($name, $path = null, $domain = null, $secure = null, $httponly = null)

The delete method deletes a parameter from the $_COOKIE array.

When a cookie was created under a specified path or/or domain etc. you must also supply this to the delete function.

Static Yes
Parameters
Param Default Description
$name Required Remove a cookie item.
$path
null
The path on the server in which the cookie will be available on.
$domain
null
The domain that the cookie is available to.
$secure
null
Set to true if you only want to transmit cookies over secure connections.
$httponly
null
Allow only transmit of cookies over HTTP, disabling Javascript access.
Returns void
Example
// Delete normal cookie
\Cookie::delete('theme');

// Set a cookie with a path
\Cookie::set('theme', 'fuel', null, '/account/');

// Delete that cookie
\Cookie::delete('theme', '/account/');