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: Array Method, and Discrete Parameters:
Array Method
Using this method, an associative array is passed to the first parameter:
$cookie = array(
'name' => 'CookieName',
'value' => 'The Value',
'expire' => '86500',
'domain' => '.some-domain.com',
'path' => '/',
'prefix' => 'myprefix_',
'secure' => false,
);
set_cookie($cookie);
Notes:
Only the name and value are required.
Expiration:
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.
Passing non numeric value for expire param will cause the default value, as configured in $config['cookie_lifetime'], to be used.
To delete a cookie set it with the expiration set to a negative number (delete_cookie() is preferred).
Domain:
For site-wide cookies regardless of how your site is requested, add your site FQDN starting with a period, like this: .your-domain.com
If you do not pass the domain param, or pass it as null, the default option as configured in $config['cookie_domain'] will be used. Passing an empty string will cause no domain to be set.
Path:
If you do not pass the path param, or pass it as null, the default option as configured in $config['cookie_path'] will be used. Passing an empty string will cause no path to be set.
Prefix:
The prefix is only needed if you need to avoid name collisions with other identically named cookies for your server.
If you do not pass the prefix param, or pass it as null, the default option as configured in $config['cookie_prefix'] will be used. Passing an empty string will cause no prefix to be used.
Secure:
If this cookie can ONLY be sent over HTTPS (SSL), set this value to true.
Discrete Parameters
If you prefer, you can set the cookie by passing data using individual parameters:
set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);
get_cookie()
Lets you fetch a cookie. The first parameter will contain the name of the cookie you are looking for:
get_cookie('some_cookie');
The function returns FALSE (boolean) if the item you are attempting to retrieve does not exist.
The second optional parameter is the prefix to use in front of the name. If you do not pass this param, or pass it as null, the default site cookie parameter as configured in $config['cookie_prefix'] will be used. Pass this as empty string to use no prefix.
get_cookie('some_cookie','myprefix_');
The third optional parameter lets you run the data through the XSS filter. It's enabled by setting the third parameter to boolean TRUE;
get_cookie('some_cookie','myprefix_', TRUE);
-OR-
get_cookie('some_cookie',null, TRUE);
delete_cookie()
Lets you delete a cookie. Unless you've set a custom path or other values, only the name of the cookie is needed:
delete_cookie("name");
This function is fairly identical to set_cookie(), except that it does not have the value and expiration parameters. You can submit an array of values in the first parameter or you can set discrete parameters. The other difference is that the final parameter, $remove_live, flags the function to delete the value from the $_COOKIE array along with sending the expiration trigger to the browser. If you would like to keep this value in the array, pass discrete parameters and explicitly set this flag to FALSE.
delete_cookie($name, $domain = null, $path = null, $prefix = null, $remove_live = TRUE)