HTTP cache control headers

I had to do two things today. One was to make sure that in production all of my resources were cached. The other was to make sure that in development none of my resources were cached. I ended up with these two functions, which seem to be doing the trick to disable/enable caching:

function set_nocache() {
  header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
  header("Cache-Control: post-check=0, pre-check=0", false);
  header("Pragma: no-cache");
}

function set_cache() {
  header('Expires: ' . gmdate( 'D, d M Y H:i:s', time()+31536000 ) . ' GMT');
  header('Cache-Control: max-age=31536000');
}

Reference: Completely disable any browser caching.

GeoIP with MaxMind’s GeoLite in PHP

You can download the GeoLite Country database from GeoLite Free Downloadable Databases and you can grab the MaxMind GeoIP PHP API.

Once you have the GeoIP.dat file and the geoip.inc PHP file you’re good to go. E.g.:


  require_once __DIR__ . '/geoip/geoip.inc';

  $gi = geoip_open( __DIR__ . '/geoip/GeoIP.dat", GEOIP_STANDARD );

  $country_code = geoip_country_code_by_addr( $gi, $host );
  $country_name = geoip_country_name_by_addr( $gi, $host );

  geoip_close( $gi );

Are Prepared Statements a waste for normal queries? (PHP)

I was interested to know if using prepared statements was a waste of time if the statement was only going to be used once. I found Are Prepared Statements a waste for normal queries? (PHP) on StackOverflow and basically came away with the understanding that it’s a pretty low overhead and it’s good for security so you might as well use prepared statements all the time.