2

For instance, if you exit your Yahoo mail and then click the back button, it will not load the last page, it will redirect you to the login page.

I have to do this with my PHP code, I'm using CodeIgniter.

Some friends told me to disable caching but that will be a bad thing because I have a lot of images in my system and it would be bad to download them every time.

How do I do this with PHP?

6
  • 3
    Disable caching for your PHP page and enable caching for images or put them on a CDN.
    – Thai
    May 16, 2011 at 12:21
  • 3
    You seem to think that if you disable caching on a page, that everything that that page references through src attributes also is not cached. But this is not true. You only disable the caching for that specific page. Scripts, images and iframes will be requested by the browser separately and thus have their own caching attributes.
    – Mel
    May 16, 2011 at 12:33
  • And with "crude and hasty", you mean "ineffective"? Mar 16, 2012 at 14:00
  • by crude and hasty I mean boss didn't care about "that's gonna take a while to do it right" and said "you got until friday". What i mean is that i don't think it's an overall elegant solution to the problem but it works and the higher-ups accepted the compromise
    – Bogdan
    Mar 16, 2012 at 14:03
  • Also, I did use php and javascript. Mostly php for the restrictions.
    – Bogdan
    Mar 16, 2012 at 14:04

3 Answers 3

5

Disable caching of your page with the following code :

http://php.net/manual/en/function.header.php

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>
4
  • But disabling cache isn't a bad thing?
    – user745235
    May 16, 2011 at 12:28
  • 2
    @Gerep: This way, you only disable caching of the page itself, not of the images. Moreover, I suspect you don't want the browser to cache confidential information? May 16, 2011 at 12:30
  • It will reload your images every time that user comes on that page and that is not a good thing. May 16, 2011 at 12:30
  • @Marcel Yes, I have to take care with the confidential information...but since it will not cache only the page, its a temporary solution...thanks a lot =)
    – user745235
    May 16, 2011 at 12:32
1

Try these:

<?php
header("Expires: Fri, 01 Jan 2010 05:00:00 GMT");
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' ); 
?>
0

The simple answer to avoid caching on the client browsers is to configure the Cache-Control HTTP response header.

http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Avoiding_caching

However, I don't have a PHP snippet with me to show you how to do it exactly. It should as simple getting the HTTP response object, and setting a header "Cache-Control" with value "no-store, must-revalidate"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.