Codeigniter Cookie Helper contains the functions that are used in working with cookies. Here in this tutorial we are going to explain how you can create cookies using this helper. Let us go step by step to learn about the Cookie Helper.
Cookie Helper in Codeigniter Example
Load This Helper
Before using the helper function you must load this helper as below –
Cookie Helper in Codeigniter Example:
$this->load->helper('cookie'); |
After Loading this function you can use it’s functions.
Cookie Helper Functions
Let us go through the available functions in this helper.
set_cookie Function
Syntax of set_cookie function
Codeigniter set_cookie function syntax:
set_cookie($name, $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE); |
Parameters:
- $name (mixed) – Name of Cookie.
- $value (string) – Cookie value.
- $expire (int) – Number of seconds until expiration.
- $domain (string) – Cookie domain ex.- www.tutorialsplane.com.
- $path (string) – Cookie path.
- $prefix (string) – Cookie name prefix.
- $secure (bool) – To send the cookie using HTTPS.
- $httponly (bool) – To hide the cookie from JavaScript.
get_cookie Function
Syntax of get_cookie function
Codeigniter get_cookie function syntax:
get_cookie($index, $xss_clean = NULL); |
Parameters:
- $index (string) – Name of Cookie.
- $xss_clean (bool) – If you want to apply xss filtering to returned value set true else it will be false by default.
Returns:
Returns cookie value. If value not found it will return NULL.
delete_cookie Function
Syntax of delete_cookie function
Codeigniter delete_cookie function syntax:
delete_cookie($index, $domain, $path, $prefix); |
Parameters:
- $name (mixed) – Name of Cookie.
- $domain (string) – Cookie domain ex.- www.tutorialsplane.com.
- $path (string) – Cookie path.
- $prefix (string) – Cookie name prefix.
Returns:
Returns void.
delete_cookie Example
If you do not want to specify other params you can delete the cookies as below –
Codeigniter delete_cookie Example:
delete_cookie('my_cookie'); //my_cookie name of the cookies |
Where my_cookie is name of the cookie.
Examples
Let’s have look over examples on set, get and delete cookie.
Codeigniter Set Cookie Example
You can set cookie value simply as below –
Codeigniter Set Cookie Example :
public function setMyCookie(){ $this->load->helper('cookie'); $cookie = array( 'name' => 'username', 'value' => 'test@tutorialsplane.com', 'expire' => time()+86500, 'domain' => 'www.tutorialsplane.com', // You can add .localhost for local machine 'path' => '/', 'prefix' => 'tutorialsplane_', ); set_cookie($cookie); } |
Codeigniter Get Cookie Example
You can get cookie value simply as below –
Codeigniter Get Cookie Example :
public function getMyCookie(){ $this->load->helper('cookie'); //get_cookie('prefix_name'); $cookieVal = get_cookie('tutorialsplane_username'); echo $cookieVal; } |
Codeigniter Delete Cookie Example
You can delete cookie value simply as below –
Codeigniter delete Cookie Example :
public function deleteMyCookie(){ $this->load->helper('cookie'); // delete cookie example $cookie = array( 'name' => 'username', 'value' => '', 'expire' => '0', 'domain' => 'www.tutorialsplane.com', // You can add .localhost for local machine 'prefix' => 'tutorialsplane_' ); delete_cookie($cookie); } |
The above example will delete the cookie set in the above example.