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