CodeIgniter 4 Cookie Helper Tutorial

Share this Article
Reading Time: 7 minutes
11,679 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.

Learn More –

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.

CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Environment (.env) Setup

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

Either we can do via renaming file as simple as that. Also we can do by terminal command.

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.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

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

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.

Was this post helpful?

PHP Web Development Courses on Offer in just $4
PHP Web Development Courses on Offer in just $4