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.
get($name, $default = null)
The get method allows you to read a $_COOKIE variable.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$name |
Required |
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)
The delete method deletes a parameter from the $_COOKIE array.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$name |
Required |
Remove a cookie item. |
|
Returns |
void |
Example |
Cookie::delete('theme');
|