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. For example, when user logged in to the 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 itself of the Codeigniter. So create the core class MY_Output which extends CI_Output and put the core class under “application/core” containing the following code.


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

 * Description of MY_Output
 * @author Soumitra Roy Sarkar
class MY_Output extends CI_Output {

    function nocache() {
        $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');


/* End of file MY_Output.php */
/* Location: ./application/core/MY_Output.php */

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 particular that 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-store to tell the browser that it can cache but before that it must validate with the server.

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

I use to place this in my controller’s constructor for pages where user validation is required.

For example,


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

 * Description of signin
 * @author
class Signin extends CI_Controller {

    private $error;

    function __construct() {
        //MY_Output class's nocache() method
    //more code
/* End of file signin.php */
/* Location: ./application/controllers/signin.php */

In the above code we call the method nocache() inside the controller constructor to disable browser cache easily in Codeigniter. You can write other methods as usual in the controller. You have to call this nocache() method in those controllers where you don’t want your browser cache the data.

Thank’s for reading.


I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

2 thoughts on “How to disable browser cache easily in Codeigniter

  1. Hi,
    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,

Leave a Reply

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