This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

This holiday season, maximize your sales through enhanced website performance and 24/7 caring support. SAVE 30% NOW

How To Set Cookies In Codeigniter

Updated on  5th September

3 Min Read
Reading Time: 3 minutes

Cookies are small data files that store specific information about the sessions. This could login status and cart contents. These files are stored in a “Cookies” folder (the name could differ).

Creating and setting cookies in Codeigniter is easy because of a native helper class called (very appropriately) Cookie helper class. This class has all the necessary methods for creating, setting and managing cookies in your Codeigniter projects.

You might also like: How To Host CodeIgniter PHP On Cloud Using SSH

Creating the Cookies

To highlight how the Cookies helper class works, I will create a controller cw_cookies.php. This controller will have the following code:


The above code sets the cookies through

The helper is loaded using:

Pass information in Cookies

CodeIgniter offers two ways of passing information in the cookies.

The method is the Array Method that involves passing an associative array as a parameter in the set(). A typical example is:

This structure is set using the following:

In this method, only the first two parameters (name and value) are required. The expiration of the cookie is set through the expire parameter. This is the number of seconds the developer wishes to retain the cookie. If this duration is set to zero, the cookie will be removed once the browser is closed. The cookie can be made site-wide by adding a period before the domain name (

The second method involves passing individual parameters rather than the entire array. This method allows more control over the process of setting cookies because the developer could decide the parameters for the individual cookies. The prefered syntax is:

You might also like: Pass Data From One Function To Another In Same Codeigniter Controller

Fetch a Cookie

Once a cookie has been created, the next step is the process of fetching the cookie. The standard syntax is:

If the function cannot find the requested (first) parameter, it will return boolean FALSE.  (boolean)  if the item you are attempting to retrieve does not exist. The second optional parameter (when set to TRUE) will run the data through the XSS filter.


In this article, I have discussed how to set a cookie in CodeIgniter projects deployed on any PHP MySQL Hosting. The process is really easy and the developers have the option to set the cookies in two ways. Once the cookies have been set, fetching them is a breeze.

Share your opinion in the comment section. COMMENT NOW

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Get Our Newsletter
Be the first to get the latest updates and tutorials.