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.
Like this article? Stay up to date with new articles and content. It’s free, and we won’t sell your information.
SIGN UP TODAY by entering your primary email address below to guarantee you won’t miss anything.
