Cookie management in CodeIgniter

Cookie management in CodeIgniter


  • A cookie is most commonly used to identify a user.
  • It is a small file stored by the server on the client’s computer.
  • Codeigniter provides a cookie helper to handle the cookies.
  • The Cookie Helper file contains functions that assist in working with cookies.

Related Topics:

Codeigniter Interview Questions
Session management in Codeigniter
Tempdata in Codeigniter

Loading this Helper:

The following code is used to load the cookie helper.


Available Functions:

1 set_cookie(): This helper function is used to set a browser cookie.


set_cookie($name[, $value = ''[, $expire = ''[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = FALSE[, $httponly = FALSE]]]]]]]]);

Parameter Description:

  • $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:
  • $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

The return type of this function is void.

2 get_cookie(): This function is used to get the browser cookie.


get_cookie($index[, $xss_clean = NULL]]);

Parameter Description:

  • $index (string) − Cookie name
  • $xss_clean (bool) − Whether to apply XSS filtering to the returned value

The return type of this function is mixed.

3 delete_cookie(): This function is used to delete a cookie.


delete_cookie($name[, $domain = ''[, $path = '/'[, $prefix = '']]]]);

Parameter Description:

  • $name (string) − Cookie name
  • $domain (string) − Cookie domain (usually:
  • $path (string) − Cookie path
  • $prefix (string) − Cookie name prefix

The return type of this function is void.


Step 1 Open the application/views directory and create a new view file cookie_view.php.

<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
<script src=""></script>
<script src=""></script>

<div class="container">
<h2>cookie example</h2>

<a href = 'Cookie_controller/display_cookie'>Click Here</a> to view the cookie.<br>
<a href = 'Cookie_controller/deletecookie'>Click Here</a> to delete the cookie.



Step 2 Open the application/controller directory and create a new controller Cookie_controller.php.

class Cookie_controller extends CI_Controller {

function __construct() {
$this->load->helper(array('cookie', 'url')); //load cookie helper

public function index() {
set_cookie('example','cookie example on CodingTag','3600'); // set cookie example

public function display_cookie() {
echo get_cookie('example');

public function deletecookie() {


Step 3 Open the given URL into the browser.


After clicking on the first link, the cookie will be set.

After clicking on the second link, it will be deleted.

Best WordPress Hosting


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price

Leave a Reply

    Waiting for your comments