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:

Cookie Helper Functions

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

Syntax:

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

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

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

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

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

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

Pin It on Pinterest

Share This