htaccess Elite

.htaccess tutorial


All times are UTC [ DST ]





Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Dont-cache Page
PostPosted: Nov 3rd, '06, 09:43 
Offline

Joined: Nov 1st, '06, 05:19
Posts: 25
Don't Cache

Code:
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:
@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.


Top
 Profile  
 
 Post subject:
PostPosted: Nov 3rd, '06, 10:00 
Offline

Joined: Nov 1st, '06, 05:19
Posts: 25
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:
<?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).


Top
 Profile  
 
 Post subject:
PostPosted: Nov 3rd, '06, 10:01 
Offline

Joined: Nov 1st, '06, 05:19
Posts: 25
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:
<?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();
?>


Top
 Profile  
 
 Post subject:
PostPosted: Nov 3rd, '06, 10:01 
Offline

Joined: Nov 1st, '06, 05:19
Posts: 25
Quote:
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: MSNbot Media and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB