.htaccess tutorial

htaccess Elite


Dont-cache Page

Using PHP in and with htaccess

Dont-cache Page

Postby snooze » 03 Nov 2006 09:43

Don't Cache

Code: Select all
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-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");


or to suppress errors

Code: Select all
@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-store, no-cache, must-revalidate");
@header("Cache-Control: post-check=0, pre-check=0", false);
@header("Pragma: no-cache");


----------------------------------------------------------
apache_request_headers() is only available if PHP is running as an apache module. Various request header values are available in the $_SERVER array, for example:

$_SERVER["HTTP_IF_MODIFIED_SINCE"]

Gives the if modified date in "Sat, 12 Aug 2006 19:12:08 GMT" format.
snooze
 
Posts: 26
Joined: 01 Nov 2006 05:19

Postby snooze » 03 Nov 2006 10:00

If you want to enable caching of your page, you have two solutions: use Etag or use Last-Modified.

Someone has already posted here the code to use Etag. However, sometimes, it is easier (better) to use Last-Modified. The full code is here:

Code: Select all
<?php
function get_http_mdate()
{
   return gmdate("D, d M Y H:i:s",filemtime($SCRIPT_FILENAME))." GMT";
}

function check_modified_header()
{
   // This function is based on code from http://ontosys.com/php/cache.html

   $headers=apache_request_headers();
   $if_modified_since=preg_replace('/;.*$/', '', $headers['If-Modified-Since']);
   if(!$if_modified_since)
       return;

   $gmtime=get_http_mdate();

   if ($if_modified_since == $gmtime) {
       header("HTTP/1.1 304 Not Modified");
       exit;
   }
}

check_modified_header();
header("Last-Modified: ".get_http_mdate());
?>


The script checks if time from "If-Modified-Since" header is equal to current modified-time. If it is, a 304 code is returned, and PHP exits. If it is not, PHP continues normally.

You may want to change how "get_http_mdate()" function gets the time. You may want to get time from another file, or from somewhere else (like a date field on database).
snooze
 
Posts: 26
Joined: 01 Nov 2006 05:19

Postby snooze » 03 Nov 2006 10:01

How to force browser to use already downloaded and cached file.

If you have images in DB, they will reload each time user views them. To prevent this, web server must identify each file with ID.

When sending a file, web server attaches ID of the file in header called ETag.
header("ETag: \"uniqueID\");

When requesting file, browser checks if the file was already downloaded. If cached file is found, server sends the ID with the file request to server.

Server checks if the IDs match and if they do, sends back
header("HTTP/1.1 304 Not Modified");
else
Server sends the file normally.
Code: Select all
<?php

  $file = getFileFromDB();

  // generate unique ID
  $hash = md5($file['contents']);
 
  $headers = getallheaders();
 
  // if Browser sent ID, we check if they match
  if (ereg($hash, $headers['If-None-Match']))
  {
   header('HTTP/1.1 304 Not Modified');
  }
  else
  {
   header("ETag: \"{$hash}\"");
   header("Accept-Ranges: bytes");
   header("Content-Length: ".strlen($file['content']));
   header("Content-Type: {$mime}");
   header("Content-Disposition: inline; filename=\"{$file['filename']}\";");

   echo $file['content'];
  }
  exit();
?>
snooze
 
Posts: 26
Joined: 01 Nov 2006 05:19

Postby snooze » 03 Nov 2006 10:01

If you are trying to modify headers to work with dynamic binaries such as videos or images, the new IE 7 appears to require the ETag header. You will need to make sure that you follow the specifications for how ETag works in order for your cache control to work properly. Mozilla supports the ETag header as well, but does NOT require it for caching. If you need to cache a dynamic image, video, or other binary file, and are having trouble in IE7, be sure to set your ETag and then check for the If-Not-Modified header on subsequent requests so that you can properly return the 304 Not Modified page. If you do not, then it will NEVER cache the file (as of IE version 7.0.5730.11
snooze
 
Posts: 26
Joined: 01 Nov 2006 05:19


Return to PHP and htaccess