PHP – Prevent Browser And Proxy Cache

One thing that had been always troubling me was the proxy cache. There are many processes server data needs to go through before it appears on our screen. If you use Yahoo! Web Hosting or BlueHost in Asia, you may find that after you upload a new web page to the remote shared hosting server, it takes a long time to see any real changes. Many people blame the hosting company on this, actually this is caused by the proxy the web page goes through, not the server’s problem.

A server can explicitly inform the browser, and any proxy caches that might be between the server and browser, of a specific date and time for the document to expire. Proxy and browser caches can hold the document until that time or expire it earlier. Repeated reloads of a cached document do not contact the server. However, an attempt to fetch an expired document does contact the server.

So no matter what you do (rewrite the file a million times through ftp or clear all your cache), you won’t get the new updated page displayed, because the proxy in the middle of the server and your browser does the ‘evil’ caching. Caching can make web developers frustrated, but once you know how it works and control it, it’s not that evil at all.

About how it works, which is a long story, has been brilliantly told by The World Wide Web Consortium (W3C) – HTTP. But that’s a long story as I said, and you need to read for days to get the piece of information you need. Finally, I found a quick and dirty version of it – simple and plain code. Below is the best way to prevent a browser or proxy cache from storing your HTML document:

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");
<html dir="ltr" lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

The only catch to setting headers is that you must do so before any of the body is generated. This means that all calls to header( ) (or setcookie( ), if you’re setting cookies) must happen at the very top of your file, even before the tag.

Although the code above works fine, I still think it’s a good idea to understand how HTTP works.

Be Sociable, Share!

About Shi Chuan

I am a web developer.
This entry was posted in PHP Core, PHP Hacks and tagged , , , , , , , . Bookmark the permalink.

6 Responses to PHP – Prevent Browser And Proxy Cache

  1. glype proxy says:

    I’m extremely impressed with your writing abilities as smartly as with the structure in your blog. Is this a paid topic or did you modify it yourself? Either way keep up the excellent high quality writing, it?s rare to look a nice weblog like this one today..

  2. Proxy Sites says:

    It?s really a great and helpful piece of info. I am happy that you simply shared this useful info with us. Please stay us up to date like this. Thank you for sharing.

  3. What?s Going down i am new to this, I stumbled upon this I’ve found It absolutely helpful and it has aided me out loads. I hope to contribute & aid other users like its aided me. Good job.

  4. I’ve been surfing online more than three hours nowadays, but I never discovered any interesting article like yours. It is beautiful value enough for me. In my view, if all website owners and bloggers made just right content material as you probably did, the web shall be much more useful than ever before.

  5. hello there and thanks to your info ? I’ve definitely picked up anything new from proper here. I did alternatively expertise several technical points the usage of this website, as I skilled to reload the web site lots of times prior to I may just get it to load correctly. I had been pondering in case your web host is OK? Now not that I’m complaining, however sluggish loading cases times will sometimes have an effect on your placement in google and can damage your high-quality score if advertising and marketing with Adwords. Well I am adding this RSS to my email and can glance out for a lot extra of your respective interesting content. Make sure you update this once more soon..

  6. w3 says:

    Thank you a lot for sharing this with all folks you really recognise what you’re talking approximately! Bookmarked. Kindly also visit my web site =). We will have a hyperlink trade arrangement between us

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>