CodeIgniter Cookie Management
A cookie is a piece of information contained in a very small text file that is stored in your Internet browser or elsewhere on your hard drive. Cookies allow a website to identify a user's device whenever that user returns to the website and are commonly used in order to make websites work more efficiently.
Cookie Helper
The Codeigniter Cookie Helper file contains functions that assist in working with cookies.
- Loading Codeigniter Cookie Helper
- Available Codeigniter Cookie Functions
Loading Codeigniter Cookie Helper
This helper is loaded using the following code:
$this->load->helper('cookie');
Available Codeigniter Cookie Functions
The following functions are available:
- set_cookie(some argument)
- get_cookie(some argument)
- delete_cookie(some argument)
set_cookie(cookie_name,cookie_value,cookie_expire,cookie_secure);
Parameters:
- $name (mixed) – Cookie name or associative array of all of the parameters available to this function
- $value (string) – Cookie value
- $expire (int) – Number of seconds until expiration
- $domain (string) – Cookie domain (usually: .yourdomain.com)
- $path (string) – Cookie path
- $prefix (string) – Cookie name prefix
- $secure (bool) – Whether to only send the cookie through HTTPS
- $httponly (bool) – Whether to hide the cookie from JavaScript
set_cookie('cookie_name','cookie_value','3600');
Return type:void
This Codeigniter helper function gives you friendlier syntax to set browser cookies. Refer to the Input Library for a description of its use, as this function is an alias for
get_cookie(cookie_name)
Parameters:
- $index (string) – Cookie name
- $xss_clean (bool) – Whether to apply XSS filtering to the returned value
Returns: The cookie value or NULL if not found
Return type:mixed
echo get_cookie('cookie_name');
delete_cookie(cookie_name)
Parameters:
- $name (string) – Cookie name
- $domain (string) – Cookie domain (usually: .yourdomain.com)
- $path (string) – Cookie path
- $prefix (string) – Cookie name prefix
Return type:void
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('cookie_name');