prevent browser caching with PHP

<?php

function bustcache()
{
  if(headers_sent())
  {
    trigger_error('Already sent HTTP headers, not busting the cache', E_NOTICE);
  }
  else
  {
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
    header('Cache-Control: no-cache, must-revalidate');
    header('Pragma: no-cache');
  }
}

?> 

The above is a very simple function for preventing a browser from caching content.  This isn't recommended on static sites, but is exceptionally helpful when building dynamic applications. Simply call this function before any output.

October 21, 2011 at 3:48am / Code, General Web Programming, PHP 

Comments On This Entry

I am ttolaly wowed and prepared to take the next step now.

Ladainian @ 10:42:17 AM, Sunday, October 23, 2011

Compose New Blog Comment
    required = required field         warning HTML and URL's are not allowed

[back to main]