Disable browser cache easily with CodeIgniter

I have a project where users are validated by session data. When the user logs out I destroy the session and redirect them to the log-in page, but this does not prevent them from clicking the browser’s “back” button, in which case they still see the data.

My first solution was to set headers with PHP’s header() function. Then I decided to use CodeIgniter’s setheader() function from the Output library (no real difference, just using CI methods when possible). Finally, I decided the best way to do this would be to extend the Output library itself. This way I am not repeating multiple function calls in each controller. Here is end-result…

Create MY_Output.php in “application/libraries” containing the following code…

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

class MY_Output extends CI_Output {

    function nocache()
    {
        $this->set_header('Expires: Sat, 26 Jul 1997 05:00:00 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 */

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

$this->output->nocache();

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

Happy coding :)

Bookmark and Share

10 thoughts on “Disable browser cache easily with CodeIgniter

    1. Robert Mullaney Post author

      This function only disables the cache for the page.

      In order to control caching for linked objects (scripts, css, images, etc) you need to pass them through a loader that calls this function before kicking out the file’s content. Or, you modify the tags (see below) to prevent caching. This method will impact load times as the example image and stylesheet are never cached…

      <link rel="stylesheet" href="mystylesheet.css?<?php echo time(); ?>">
      <img src="myimage.jpg?<?php echo time(); ?>">
      Reply
  1. Roark

    Great tip thanks Robert,

    Works perfectly but remember that core class extensions go into the “application/core” folder not “application/libraries” as you mentioned.

    Thanks!

    Reply
  2. Robert Mullaney Post author

    This code was written during 1.7 when they resided in “application/libraries” … It should be in the “application/core” for CI 2.0+

    Note: If you have the “application/core” folder, place it there. If not, you are most likely using an older distribution and it needs to go in the libraries folder ;)

    Hint: An easy way to check your CodeIgniter version is by placing < ?php echo CI_VERSION; ?> in a view. The CI_VERSION constant has been available since 1.5.4, before then you would be looking for the APPVER constant.

    Happy Coding!

    Reply
  3. Robert Mullaney Post author

    On Wed, 30th Nov 2011, I provided examples using multiple calls to PHP’s time() function. I suggest reducing duplicate function calls by creating a $timestamp variable to use instead. There’s no sense calling a function multiple times when saving a single call to a variable will yield the same results and reduce unnecessary overhead.

    Reply
  4. Paul

    I am trying to disable caching for a image form upload which refreshes the uploaded image. The database value changes and I am outputting the image using imagename.jpg? however it works the first time but thereafter this method seems to fail.

    Reply
  5. Joel

    You are a life saver. I had an issue with Codeigniter where it would default to caching all my pages. All the changes to the database would not be reflected to the user unless they refreshed. Now it’s fixed! THANK YOU!

    Reply

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>