1. Home
  2. Knowledge Base
  3. CodeIgniter
  4. CodeIgniter – Cookie Management

CodeIgniter – Cookie Management

Cookie is a small piece of data sent from web server to store on client’s computer. CodeIgniter has one helper called “Cookie Helper” for cookie management.

Syntax set_cookie($name[, $value = ”[, $expire = ”[, $domain = ”[,$path = ‘/’[, $prefix = ”[, $secure = FALSE[, $httponly = FALSE]]]]]]]])
  • $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
Return Type void

In the set_cookie() function, we can pass all the values using two ways. In the first way, only array can be passed and in the second way, individual parameters can also be passed.

Syntax get_cookie($index[, $xss_clean = NULL]])
  • $index (string) − Cookie name
  • $xss_clean (bool) − Whether to apply XSS filtering to the returned value
Return The cookie value or NULL if not found
Return Type mixed

The get_cookie() function is used to get the cookie that has been set using the set_cookie() function.

Syntax delete_cookie($name[, $domain = ”[, $path = ‘/’[, $prefix = ”]]]])
  • $name (string) − Cookie name
  • $domain (string) − Cookie domain (usually: .yourdomain.com)
  • $path (string) − Cookie path
  • $prefix (string) − Cookie name prefix
Return Type void

The delete_cookie() function is used to delete the cookie().


Create a controller called Cookie_controller.php and save it atapplication/controller/Cookie_controller.php

   class Cookie_controller extends CI_Controller { 
      function __construct() { 
         $this->load->helper(array('cookie', 'url')); 
      public function index() { 
      public function display_cookie() { 
         echo get_cookie('cookie_name'); 
      public function deletecookie() { 

Create a view file called Cookie_view.php and save it atapplication/views/Cookie_view.php

<!DOCTYPE html> 
<html lang = "en">
      <meta charset = "utf-8"> 
      <title>CodeIgniter View Example</title> 
      <a href = 'display'>Click Here</a> to view the cookie.<br> 
      <a href = 'delete'>Click Here</a> to delete the cookie. 

Change the routes.php file in application/config/routes.php to add route for the above controller and add the following line at the end of the file.

$route['cookie'] = "Cookie_controller"; 
$route['cookie/display'] = "Cookie_controller/display_cookie"; 
$route['cookie/delete'] = "Cookie_controller/deletecookie";

After that, you can execute the following URL in the browser to execute the example.


It will produce an output as shown in the following screenshot.


Was this article helpful?

Related Articles