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.

📣 Join our live AMA on the Future of Page Builders with Brizy's CEO! Register Now →

How To Set Cookies In Codeigniter

Updated on December 22, 2021

3 Min Read
codeigniter set cookie

Cookies Codeigniter 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).

It can also be said that Cookies Codeigniter is a set of information sent from a web server to store on a user’s computer. CodeIgniter has a helper called “Cookie Helper” for cookie management.

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

Creating the Cookies

An HTTP cookie is controlled by a few specifications that have to be followed in order to be accepted by the browsers. Thus, when making or altering certain attributes of the Cookie, these are validated in order to check in case these follow the specifications.

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

<?php

if ( ! defined('BASEPATH')) exit('Stop Its demostrate how to set cookie');

class cw_cookies extends CI_Controller {

   function __construct()

   {

       parent::__construct();

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

   }



   function set()

   {

       $cookie= array(

           'name'   => 'Cloudways Cookie',

           'value'  => 'This is Demonstration of how to set cookie in CI',

           'expire' => '3600',

       );

       $this->input->set_cookie($cookie);

       echo "Congragulatio Cookie Set";

   }



   function get()

   {

       echo $this->input->cookie('Cloudways Cookie',true);

   }

}

The above code sets the cookies through

$this->input->set_cookie()

The helper is loaded using:

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

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

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:

$cookie = array(

   'name'   => 'CW Cookie',

   'value'  => 'This is Demonstration of how to set cookie in CI',

   'expire' => '3600',

   'domain' => 'your-domain-name',

   'path'   => '/',

   'secure' => TRUE

);

This structure is set using the following:

$this->input->set_cookie($cookie);

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 (your-domain.com).

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:

$this->input->set_cookie($first_parameter, $second_parameter, $third_parameter, $fourth_parameter);

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

$this->input->cookie('First Parameter',true);

If the function cannot find the requested (first) parameter, it will return a 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.

Conclusion

This article can be summarized as; To utilize Cookies in Codeigniter, we have to use its helper class. The Cookie Helper record contains capacities that help in working with treats.

I have discussed how to set a cookie in CodeIgniter projects deployed on any PHP 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

Share This Article

Inshal Ali

Inshal is a Content Marketer at Cloudways. With background in computer science, skill of content and a whole lot of creativity, he helps business reach the sky and go beyond through content that speaks the language of their customers. Apart from work, you will see him mostly in some online games or on a football field.

×

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

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now