×

CodeIgniter Cookie Helper

A cookie is a small set of files sent from the web server to the end-user system. In Helpers, the cookie helper file has some predefined functions that are used to the set cookie, get the cookie, and delete the cookie.

Before using the cookie helper function in an application, you must load the helper.

Loading the helper

It is used to load the helper class and pass the helper function in the controller’s method.

Syntax:

$this->load->helper(‘cookie);

Cookie Helper Functions

set_cookie function: This function is used to set the cookie in a web application.

Syntax:

set_cookie($name, $value = ‘ ‘, $expire = ‘
’, $domain = ‘ ‘, $path = ‘/ ‘, $prefix = ‘ ‘ , $secure = FALSE, $httponly =
FALSE);

Description of Parameters:

$name: It is used to show the name of the cookie.

$value: It contains message or information of the cookie that you want to show.

$expire (seconds): It tells about the time for which the cookie information remains in the system before expiration.

$domain: It represents the domain on which you want to set cookie.

For example, www.tutorialandexamples.com or in your local server like www.localhost.com

$path: It shows the path of the cookie.

$prefix: As the names suggest, it allows you to add any strings as a prefix to the cookie $value.

$secure (bool): It is used to send the cookie in the https platform. It contains the value in Boolean forms like TRUE and FALSE. By default, it is FALSE.

$httponly (bool): It is used to hide the cookie from JavaScript. It also contains the value in Boolean form like TRUE and False.

Example: Create a Tests.php file in application/controller folder and write the following programs:

Tests.php

<?php
 class Tests extends CI_Controller {
  public function createCookie(){ 
      echo "<title> Tutorials And Examples </title>"; 
      $this->load->helper('cookie');
     // set cookie 
     set_cookie('code','Learn CodeIgniter at Tutorials and Examples','3600');
     echo "<h3> Cookie has been saved successfully. </h3>";
  }
 }
 ?> 

Let’s execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/test/. It shows the output, as shown below.

CodeIgniter Cookie Helper

get_cookie function: It is used to fetch the cookie value that you have set in the set_cookie function in your browser or system.

Syntax

get_cookie($index, $xss_clean = null);

Parameter’s Description:

$index: It contains the name of the cookie that you have set in set_cookie function. It is called by the name and display the message which you have stored in the set_cookie function.

$xss_clean: An XSS filtering is applied to the returned value; You have to set true. By default, it is False.

Example: Create a Tests.php file in application/controller folderand write the following programs:

Tests.php

<?php
 class Tests extends CI_Controller {
 public function showcookie()
      {      
         echo "<title> Tutorials And Examples </title>";  
         $this->load->helper('cookie'); 
         echo "<h2>Read Cookie</h2> ";
         echo get_cookie('code');
          }
 }
 ?> 

When you execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/showcookie. It shows the output, as shown below.

CodeIgniter Cookie Helper

delete_cookie function: The delete_cookie function is used to delete the cookie that is stored in the system.

Syntax

delete_cookie($name, $domain, $path,
$prefix);

Parameters description:

$name: It defines the name of the cookie that you want to delete from your system.

$domain: It defines the domain name from which you want to delete the cookie.

$path: It defines the path of the cookie that is stored in the system.

$prefix: It tells that if you have added any string before the name of the cookie to delete the cookie, you have to define the prefix name of the string before the cookie name.

Example: Create a Tests.php file in application/controller folderand write the following programs:

Tests.php

 <?php
 class Tests extends CI_Controller {
 public function delCookie()
      {  
  $this->load->helper('cookie');
          echo "<title> Tutorials and Examples </title>";
           delete_cookie('code');
          echo "<h2> Cookie has been deleted <h2>";
      }
 }
 ?> 

When you execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/delCookie. It shows the output, as shown below.

CodeIgniter Cookie Helper

Related Topics

CodeIgniter Benchmarking Class

The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking...

7 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

CodeIgniter URI Class

URI Class The CodeIgniter contains a URI class that is used to retrieve information from URI strings. It also provides suitability to get information from the re-routed segments. The system automatically initializes this...

6 minutes read.

CodeIgniter Controller

As its name suggest that it is a Controller that controls the web application, and it works as an intermediary between the Models and the View. A controller takes requests from the user...

3 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

CodeIgniter Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

7 minutes read.

CodeIgniter Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities...

12 minutes read.

Download Helper Codeigniter

Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc. Loading...

2 minutes read.

How to remove an index.php file from the Codeigniter URLs?

As we know, a URL in CodeIgniter is designed in such a way that it would be search engine and human friendly, too, rather than using a standard approach to create a...

2 minutes read.

CodeIgniter Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

12 minutes read.

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute read.

CodeIgniter Language Class

Language Class The CodeIgniter’s language class contains the various functionality that is used to access the language-specific files and text line for the purposes of internationalization. This class provides you suitability to add or...

4 minutes read.

CodeIgniter Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

10 minutes read.

CodeIgniter Output class

Output class The CodeIgniter framework contains a core class that is an Output class. It  includes the various functions that are used to send the final web page to the requesting browser. In...

8 minutes read.

CodeIgniter Smiley Helper

The smiley helper file contains smiley images that are used in the application for showing emotions and comments while chatting with others. Load a Smiley Helper Before using Smiley Helper in the CodeIgniter application, you...

4 minutes read.

CodeIgniter Inflector Helper

The inflector helper file contains some predefined function that allows users to change their English words into plural, singular, and camel case, etc. Loading an Inflector Helper Before using an inflector helper function, you must...

3 minutes read.

CodeIgniter String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

7 minutes read.

CodeIgniter Cookie Helper

A cookie is a small set of files sent from the web server to the end-user system. In Helpers, the cookie helper file has some predefined functions that are used to the...

4 minutes read.

CodeIgniter Number helper

The number helper file contains some predefined function that deals with numeric data to display numbers in bytes format. Load a Number Helper You must load a number helper file in the controller to perform...

3 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

11 minutes read.