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:

  1. 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.
  2. 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.
  3. 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.
  4. 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).
  5. 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.

Comments? Send feedback for this page here (512 characters).
(If you want a response, please include an email address)

Subscribe to RSS Feed Subscribe to Podcast



For by grace are ye saved through faith; and that not of yourselves: it is the gift of God: Not of works, lest any man should boast. For we are his workmanship, created in Christ Jesus unto good works, which God hath before ordained that we should walk in them. (Ephesians 2:8-10 KJV)