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” for CI 1.7.x or “application/core” for CI 2.x with 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 */

You might need to change the name of the file depending on your “subclass_prefix” setting in “application/config/config.php”. This guide assumes your installation uses the default value shown below.

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

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 🙂

29 thoughts on “Disable browser cache easily with CodeIgniter”

    1. 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(); ?>">
  1. 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!

  2. 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 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!

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

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

  5. 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!

  6. Hello,

    I have a database application on CodeIgniter. My problem is when I doing any actions then my view shows previous data. It’s updated when I reload page manually!

  7. Two years down the line, this is still helpful. Happy coding indeed and God bless. It helped me especially on Chrome that kept loading data from cache.

  8. Hey.. Even I am facing the same problem with browser caching.
    and one thing I didn’t understand even though I login through incognito mode I am able to view my secure page by pressing back button how this is possible..? this problem has been occurring before implementation of your codes but after implementing above code it has been throwing an error.
    I did exactly what you have explained but that has been throwing an error-“call to undefined method ci_output::nocache”
    I am using 2.2.2 version. I am kind of new to this frame work could you please help me to fix this error ..?
    any suggestion would be very helpful…thank you..

    1. To be honest, I don’t remember which version I was writing for back when this was posted. It might have be pre-CI2, but I am not sure. I know in 2.x they added the core folder for certain libraries. It might just need to be placed in the newer folder. See Creating Core System Classes in the CI 2.2 user guide.

      1. Hey.. thank you for your replay..
        I moved MY_output.php file to application/core
        and now for gods sake it is not throwing any errors but the browser cache problem persist.any suggestions.?
        thank you for your time.

        1. I’ll downloaded 2.2.2 to have a look… As long as “MY_Output.php” is in “application/core” and your “application/config/config.php” has $config['subclass_prefix'] = 'MY_'; around line #111 it should work. If page caching still occurs, your host might be caching your PHP files/output (GoDaddy does, PITA).

  9. I have used a search box. Once we search something then those loaded things saved in cache. When I implement this solution to my app those things are deleted. Could you give me a solution for that to clear cache for only post data.

    1. I have not done much with CI since 2.x, but unless they significantly changed the overrides system, it should work. If it does not, let me know and I’ll take a look at their latest distribution and docs.

Leave a Reply

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