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
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
< ?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!
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!