How to disable browser cache easily in Codeigniter


This tutorial shows how to disable browser cache easily in Codeigniter. We face some kind of problems in real life when browser caches the data particularly when there are session data. Though we destroy session but still using back button users are able to view some data on browser because browser stores data into its cache. For example, when user logged in to application they can easily go back to the login page using the browser’s back button. Again when user logged out of the application they can easily navigate to the previous page using browser’s back button.

So we can disable browser cache easily using Codeigniter or other server side technologies using cache control mechanism.

So our below example will show how to achieve it easily in Codeigniter.

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

Extending Core Class

We will create a core class MY_Output which extends CI_Output and put the core class under “application/core” containing the following code.

Whenever we want to extend Codeigniter’s core class then we need to start with MY_ because this is the default prefix for our 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 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.

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

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

class MY_Output extends CI_Output {

	 * author:
	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 we 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.

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

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

Disabling 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 we 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.

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

class LoginController extends CI_Controller {

	 * author:
	 function __construct() {
        //MY_Output's disable_cache() method
	//other code

In the above code we 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.

Thank’s for reading.

Related posts

2 Thoughts to “How to disable browser cache easily in Codeigniter”

  1. Peter Marshall

    Great article! I’m trying to get this to work myself now at the moment. Our site uses PHP and CodeIgniter. Say a user is on a page, clicks “Logout”, then clicks back, Google Chrome will still display the page with the user’s data. Can this be prevented? Is Chrome doing anything outside what CodeIgniter can control? This is what I am trying to achieve. The above doesn’t seem to be working for me.

    Any tips or advice much appreciated!
    Thank you,

    1. This should be the enough but you can check according to the Codeigniter version if there is anything else you need to take care.

Leave a Comment