How to disable Browser Cache easily in CodeIgniter 3

Introduction

In this tutorial I am going to show you how to disable browser cache easily in PHP based web framework Codeigniter. It is a good idea to cache static data on browser but it may be a problem when browsers cache dynamic data. For example, when browsers cache session data. Though generally you destroy session but still using browser’s back button users are able to view some data on browser because browser stores data into its cache.

For example, when users logged in to application they can easily go back to the login page using the browser’s back button. Again when users logged out of the application they can easily navigate to the previous page using browser’s back button.

Therefore such caching mechanism in browser is control by the cache control HTTP headers. So you can disable browser cache easily using Codeigniter or other server side technologies using cache control mechanism by sending appropriate cache control directives in HTTP headers.

You can easily disable cache in the browser settings but it is not feasible and good idea to ask your end users to disable the cache in browsers before using the application.

Here, my solution is to extend the Output library of the Codeigniter’s core class.

Prerequisites

PHP 7.3/7.4, Apache HTTP Server 2.4, CodeIgniter 3.1.11

Project Directory

It’s assumed that you have setup PHP, Apache and CodeIgniter in Windows system.

Now I will create a project root directory called codeigniter-disable-cache anywhere in the system.

Now move all the directories and files from CodeIgniter framework into the project root directory.

I may not mention the project root directory in subsequent sections and I will assume that I am talking with respect to the project root directory.

Extend Core Class

I will create a core class MY_Output which extends CI_Output and put the core class under application/core folder containing the following code.

Whenever you want to extend Codeigniter’s core class then you need to start with a prefix MY_ because this is the default prefix for your custom class name.

The prefix is configured in the config.php file under application/config folder. If you want to have different prefix then you can change the prefix in the following line in config.php file.

$config['subclass_prefix'] = 'MY_';

When you will call your core class from other class, then you drop the prefix name from the class. For example, if you create a core class MY_Output then simply call your class as output from another class.

You will create here your own core class called MY_Output under application/core folder with the below source code.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Output extends CI_Output {

	/**
	 * author: https://roytuts.com
	 */
	function disable_cache() {
        $this->set_header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
        $this->set_header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        $this->set_header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0');
        $this->set_header('Cache-Control: post-check=0, pre-check=0', FALSE);
        $this->set_header('Pragma: no-cache');
    }
	
}

In the above code I have set http headers with past date in order to disable browser cache. So it means that the headers already expired on a particular date.

I have also specified max-age=0 to indicate that cache lives only for 0 seconds, i.e., no cache.

I have also specified cache control with no-cache, no-store and must-revalidate to tell the browser that it can cache but before that it must validate with the server.

Disable Cache

Now, all you have to do is call the method with the following code before you send any data to the browser.

Generally, best place is to put into controller’s constructor for pages where user validation is required.

Let’s say you want to disable cache for login page so that when a user already logged in and by mistake he/she clicks browser’s back button then he/she should not visit the login page again.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class LoginController extends CI_Controller {

	/**
	 * author: https://roytuts.com
	 */
	 function __construct() {
        parent::__construct();
        
        //MY_Output's disable_cache() method
        $this->output->disable_cache();
    }
	
	//other code
	
}

In the above code you call the method disable_cache() inside the controller’s constructor to disable browser cache easily in Codeigniter.

You can write other methods as usual in the controller for other business requirements.

You have to call this disable_cache() method in those controllers constructors, where you don’t want your browser to cache the data.

That’s all about how to disable cache using Codeigniter framework.

Source Code

Download

Leave a Reply

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