PHP, XHTML, MIME Type and Caching - Part II
In our previous article on PHP, XHTML and MIME type, we focused on using XHTML and sending the correct MIME type as application/xhtml+xml, as it was the future of the web. Additionally, we forced PHP to send the correct 304 Not Modified header for pages not modified to reduce bandwidth and increase your users’ experience.
In Part II, we’ll update the previous script in response to a few comments, and to comply better with standards. For all the background however, you should read the previous article to be sure you have a solid foundation on why we need this, as well as bugs and issues involving the implementation.
Changes in Version 2.0:
- Checks Last-Modified header as well as ETag. Some people noted the previous script didn’t use the If-Modified-Since header. While modern browsers support ETag so it’s not necessary to use If-Modified-Since (and less problematic due to bugs in IE), it’s easy to add.
- Uses variable $max_age_hours to easily change cache time. It’s set by default to 24 hours, but you can change it greater or lesser as your situation requires.
- Sends XML prolog for application/xhtml+xml mime-types. Also not required if you’re using UTF-8 (as you should be). However, in the event you change encoding from UTF-8 (the default), you’ll need the XML prolog.
- Added a new DOCTYPE html-strict. A debate currently rages on the benefits of XHTML versus HTML. We don’t want to enter that debate here, but just note if you choose to use HTML over XHTML, the script now allows you to do so. If using the HTML Strict doctype, it doesn’t send a Vary:Accept header for HTML as it isn’t needed (as the MIME is always text/html).
- Minor changes in headers to more conform to HTTP specs.
You can grab the code download for complete information. Be sure to read the previous article for background and the previous version. We hope this enables you to improve caching on your web site and reduce bandwidth.
Copyright © 1999-2008 Darrin Yeager. http://www.dyeager.org
This page is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License. In summary, you are free to share (copy and distribute) the work under the following conditions (see the actual license for more information):
- Attribution. You must attribute the work to the author (but not in any way that suggests that they endorse you or your use of the work). Attribution should refer back to this web page and include a copyright notice and the license terms.
- Noncommercial. You may not use this work for commercial purposes.
- No Derivative Works. You may not alter, transform, or build upon this work.