CodeIgniter 4 Cookie Helper Tutorial

Share this Article
Reading Time: 7 minutes
3,049 Views

CodeIgniter 4 is an open source PHP framework. This PHP framework we have several features available including latest modules. Helpers are those sections which is provided by framework for specific functions. Either we can use application provided default helpers as well as we can create custom helpers too to manage applications.

Cookie is a helper which is very easily described with all it’s functions. Inside this article we will see about complete concept of using CodeIgniter 4 Cookie helper. Here, we will find all functions of cookie helper.

To learn How to Create custom helper in CodeIgniter 4.

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.


What are helpers in CodeIgniter?

Helpers are the standalone functions which helps to complete specific task. Actually helpers are available in CodeIgniter application to reuse the block codes.

Here are some default helpers of CodeIgniter 4 –

You can check all available helpers at this official document of codeigniter 4.

Inside above list of helpers, we can see a Cookie helper is there. So, we understood what basically it is. Now, let’s see How can we work with Cookie helper in CodeIgniter 4?


What is Cookie in Web application?

Cookie is a part of information which is injected by application to store at client side browser. Basically the purpose of cookie is to recognize and/or manage the client activity at browser.

Cookie variables and their respective storing values will be created at server side scripting code logic and injected to client side browser. Each time when user requests to the server, cookie variables embedded with request. In this way, cookie can be received at server.

Where we find cookie variables at client system ? Have a look this image.

  • Right click and click on Inspect
  • Go to Application tab of Chrome, in different browsers this tab may be with the different name.
  • Left bottom we have cookies section, where we should see domain wise cookie details.
  • Cookie details contain several information like name, value, domain, path, expire etc.

Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Turn Development Mode On

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

// Do it to 

CI_ENVIRONMENT = development

Now application is in development mode.


Cookie Helper Available Functions

To use cookie helper in CodeIgniter 4 application, we have to first load this helper in application. Then we will have access to use it’s available functions.

According to official documentation, we have these available functions –

  • set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure $httpOnly)
  • get_cookie($key_name)
  • delete_cookie($name, $domain, $path, $prefix)

set_cookie function is used to set value in cookie.

get_cookie is the function used to get cookie value which has been set by set_cookie function.

delete_cookie function removes cookie when we pass cookie key into it.

How to load Cookie helper in CodeIgniter 4?

To load Cookie helper, we have two ways in CodeIgniter 4.

  • By using BaseController.php
  • Load in specific controller file

By using BaseController.php

BaseController.php is the parent controller class for every controller in CodeIgniter 4. This file we can find at /app/Controllers/BaseController.php

Open up this file and find this line of code into file protected $helpers = []; Needs to add cookie helper there.

So updated code will be something protected $helpers = [‘cookie’];

Loading in Specific controller

To load in any specific controller, let’s understand by taking a controller. Suppose we have Home.php controller file.

<?php 

namespace App\Controllers;

class Home extends BaseController{

	public function index()
	{
        helper("cookie");
		return view('welcome_message');
	}
}

Usage of Cookie helper functions in Application

To store value in cookie, we need to use set_cookie() function.

set_cookie("username", "online_web_tutor_blog", 3600);  

username is the cookie key name. online_web_tutor_blog is the value of cookie. 3600 is number of seconds. It indicates that cookie value what we have stored will expire after 3600 seconds.

To get cookie value what we have stored at client side, we use get_cookie() function.

get_cookie("username");

This returns the value online_web_tutor_blog because a bit step behind we have stored this.

To remove cookie value from client site we use delete_cookie() function. We need to pass cookie key inside it.

delete_cookie("username");

Cookie value also automatically removed from browser when they expire.

<?php 

namespace App\Controllers;

class Home extends BaseController{

	public function index()
	{
        helper("cookie");

        // store a cookie value
        set_cookie("username", "online_web_tutor_blog", 3600);

        // get cookie value
        get_cookie("username");

        // remove cookie value
        delete_cookie("username");

		return view('welcome_message');
	}
}

Have a look how application stores cookie value at client side.

For more information Click here to go to official website of CodeIgniter

To learn about Session Service in CodeIgniter 4, Click here.

We hope this article helped you to learn CodeIgniter 4 Cookie helper in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here