How to Work Cookie Management in Codeigniter

Cookies are small data files that store specific information about the sessions. This could login status and cart contents. CodeIgniter has one helper called “Cookie Helper” for cookie management. You may also like How to create flash messages in Codeigniter and How to Session Management in Codeigniter.

Steps For How to Work Cookie Management in Codeigniter

Step 1: Initializing a Cookie

In this step open the file located at application/config/autoload.php

$autoload['helper'] = array('cookie');

Also, You can use the following code somewhere in your controller file to load the cookies.

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

Step 2: Create Cookie

In the set_cookie() function, we can pass all the values using two ways. First value is array can be passed and second one is individual parameters can also be passed.

set_cookie('cookie_name','cookie_value','3600'); 

Step 3: get Cookie

The get_cookie() function is used to get the cookie that has been set using the set_cookie() function.

echo get_cookie('cookie_name'); 

Step 4: Delete Cookie

The delete_cookie() function is used to delete the cookie.

delete_cookie('cookie_name'); 

And if you like this tutorials please share it with your friends via Email or Social Media.

Leave a Reply

Your email address will not be published. Required fields are marked *