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 🙂
Wow, Finally i found the perfect solution for the caching issue. Thanx alot. Keep up the good work.
Hmmm, still seems to cache images and css files. Nothing changed. Any tips? I did exactly what you described above.
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…
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!
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!
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.
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.
for some reason it stripped this out but i echo’d time()
When you review the html output of the page, does it read something like…
imagename.jpg?1138618081
in the src attribute of the image tag?
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!
thanks yaar …it really worked .Hope for helping with some more good tricky stuff .
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!
After validation and processing is complete, “redirect” ( URL Helper ) to see the changes
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.
Thanks just applied you code to my pastebin project
Hi,
Tried your code to clear cache but didn`t work.
Can you be more specific? Also, did you read previous comments?
Thank you.
It’s working on CI 2.2
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..
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.
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.
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).Post updated to mention differences between 1.7.x and 2.x
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.
Do not use the nocache() method in your search controller results method
This is not working properly in my codeigniter 3.0 version 🙁
Extending classes in CI3 is handled differently. See https://codeigniter.com/user_guide/general/core_classes.html
does this works for codeigniter 3.1.3 ?
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.