Follow this Blog!

May 28, 2013

f Comment

Working HTTP Cache Control Headers For Smartphones and Tablets

Amazon When I was working on a mobile version of my website I encountered a fun problem. My goal is to tell the browser to never cache my webpages. I'd like every browser to do this - Chrome, Firefox, Internet Explorer, Safari, iPhone's browser, Android phone's browser, etc.

When I used the following HTTP cache control headers in an attempt to tell the browser not to cache my content my iPhone 4 does it anyway.

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");

What's wrong with these headers? They are recommended by the official PHP website which conform to the W3C standard. I imagine if they don't work on my iPhone they probably don't work for other types of smartphones or tablets.

Do you have the same problem where your HTTP cache control headers work on some browsers and not the others? If so read on for my solution.

I am using PHP's function header() but it can be adapted to any other programming languages easily.
The solution I found is I needed to include more comprehensive headers to accommodate hopefully most browsers out there. Here they are.

header( 'Expires: Sat, 26 Jul 1997 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' );

Now my iPhone will not cache my content and will always ask my website's server for fresh content on every HTTP request. This fix should work for other smart devices too.

If not let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
Find Michael on Google or Facebook
ADVERTISING WITH US - Direct your advertising requests to Michael