×

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 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 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 Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 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.

Form Validation Library Codeigniter

Form Validation Library The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time...

31 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.

Retrieve data from Database

Retrieve data from the Database Now we will discuss how we can retrieve data from the database that we have inserted to the employees table of the database in our previous topic “inserted...

2 minutes read.

CodeIgniter CAPTCHA Helper

A CAPTCHA (Complete Automated Public Turning test to tell Computer and Humans Apart) helper is a function that is used in web application to check whether the user is human or machine. It...

3 minutes read.

CodeIgniter Security Helper

A Security helper file contains some predefined functions that are used to protect application from unauthorized access. Loading the Helper The following syntax is used to load the security helper in the CodeIgniter application. Syntax $this-> load->...

4 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.

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.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

6 minutes read.

Structure of Codeigniter

The File structure of CodeIgniter is divided into three parts: ApplicationSystemuser_guide Application -: As the name represents Application, which contains all the main parts of the project like a controller, libraries, view, models, and others....

3 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.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 minutes read.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.

CodeIgniter HTML Helper

The HTML helper function is used in CodeIgniter to provide various functionality, such as headings, images, links, list tags, etc. Loading the HTML Helper Before using the html helper function, you must load the...

7 minutes 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 Encrypt Class

The Codeigniter provides an encrypt class used to protect confidential data stored on computer systems or data transmitted over a network. It provides two-way data encryption techniques such as Mcrypt PHP extension and...

4 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.