Forum Moderators: coopster

Message Too Old, No Replies

php if-modified-since header

static site.. only use includes

         

davidpbrown

6:09 pm on Oct 10, 2003 (gmt 0)

10+ Year Member



I use PHP for includes to generate ~a static site.

I'm a little out of my depth having read GoogleGuys request for use of "If Modified Since" [webmasterworld.com] in the HTTP headers return and Sasquatch's suggestion that using "PHP - You are responsible for sending this header using the header() function.".

Recognising the benefit if I can, I'm struggling, for all my reading, to find a simple way to affect all headers without pasting PHP headers into every file.

Is it possible to suggest Modified-dates are either a set date for the whole site, or individually for every page to be the date modified of the core page, or else latest of the include files?

?
davidpbrown

jamie

8:50 pm on Oct 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi david,

this is a topic i have been looking very closely at whilst reprogramming our site.

you yourself are responsible for sending the header. you can send any type of header with php using the header function [nl.php.net]. this includes 404 headers, Last Modified headers, etc.

based on this Last Modified header (the actual time when the page in question was last updated), browsers (and search engines) can judge whether or not to reload the page from the original server, or to use the cached version which they have saved upon their last visit. they do this by comparing the last modified date of the page which you send, with the date of their own cached copy.

many php websites (see this post [webmasterworld.com]) do not bother sending a last modified date, because working out this date can be very complicated and time/resource consuming, especially when using a database.

how to do it:

basically, you need to find out the last modified dates (the time of the last update) of every bit which makes up your dynamic page, and then send the most recent one as a last modified header, which has the format:
header("Last-Modified: $your_last_modified_date goes_here");

e.g.

1) you need to compare the last modified dates of each file you use in your script. this can include your includes() and any or all of your display templates. once you have all of these dates, you have to find the most recent.

i find the easiest way to do this is to load each of these files into an array, and then loop through this array using the filemtime() [nl.php.net] function on each file to return the last modified date as a UNIX timestamp. load all of these results into a new array and rsort() the array to give you the highest version as $array[0]:

$my_array_of_included_files = array(
'my_template.inc.php',
'my_function_file.inc.php',
'$_SERVER['SCRIPT_FILENAME']'
);

foreach ($my_array_of_included_files as $key) {
$new_array[] = filemtime($key);
}
rsort($new_array);
// $new_array[0] now contains the most recent last modified date of all the included files

4) once you know this value you have to send the headers. this is done in two parts. first of all you check to see if the browser (or google) has already stored a copy of your page in its cache. if it has, then you compare the browsers' last modification date with the actual $last_modified variable. if yours is more recent, then you send a header containing this $last_modified date, which instructs the browser(google) to reload/respider the page from the server:
header("Last-Modified: $last_modified");

if however your $last_modified date is the same as the browsers (or google's) then you send a 304 (nothing has changed) header, allowing the browser to reuse the cached copy of your page, and allowing googlebot to go and spider something else on your site which _has_ been updated since its last visit. the header looks like this:
header('HTTP/1.0 304 Not Modified');

an excellent code snippet to do step 4) of this process can be found here [fishbowl.pastiche.org].

have a good read of web-caching.com. it is an invaluable resource.

when you send the correct headers, you are saving all of your visitors _lots_ of bandwidth and lots of unnecessary downloading of pages, which makes your site look like greased-lightning ;-). most people on broadband will not notice or care, but think how many dial up users you will impress with the speed of your site! not to mention allowing googlebot to grab your _really_ fresh content instead of blindly spidering everything

hope this helps.

davidpbrown

9:19 pm on Oct 10, 2003 (gmt 0)

10+ Year Member



That's very helpful.

It does suggest to me though, that I need to include a script on top every file to work this, which is unfortunate if understandable.

I was a little confused by that the server header checker suggests header codes are being sent.. although for 304 a little calculation is needed obviously.

Thanks for your help
davidpbrown

MonkeeSage

10:33 pm on Oct 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think that the way the PHP header control has been stated is a bit misleading...

You are not responsible for sending your own header -- in fact, you have to explicitly tell PHP to use quiet mode or it will always generate a standard header:

Content-type: text/html; charset=iso-8859-1
X-Powered-By: PHP/4.3.4RC1

I think that a better way of putting it would be that you yourself are responsible for sending any non-standard header entries like 'if-modified-since' or 'location'.

Jordan

jamie

6:59 am on Oct 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi david,

you needn't include an extra script. you could simply add the extra code you need to work out and send the headers in your included header.php file (or any other file).

one other thing is to remember is to send the headers before you send anything else (before any html is displayed on the screen). a way to get around this is to add ob_start() [nl.php.net] at the top of your page, and ob_end_flush() at the bottom of your page. php then takes care to send the headers in the correct order before anything else.

thanks jordan. that's what comes of when a non php-guru starts generalising :)

david, do a search in google for caching and last-modified headers with php. i know zend.com has some excellent articles on it.

MonkeeSage

7:55 am on Oct 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jamie:

I'm no guru either (not by any means)! Just started learning PHP recently actually. Only reason I know that is cause daisho told me when I was making a PHP CLI app and was trying to get it to stop printing HTTP headers to the command line ;)

Jordan