HTTP Conditional Get
I was looking into a way to cache my generated pages on the server with PHP. I couldn’t find anything to do it on the server-side, but I did find something to prevent a client from asking for the page via client-side caching.
When a web browser requests a page for the first time, it will save a copy into its local cache. If the person visits the web page a second time, the browser will send the date and time when the page in the local cache was originally retrieved. The server checks the date and responds with either a new page, or a message stating that the page hasn’t been modified since the last time the request was made.
This helps out servers to decrease how much bandwidth is being sent accross the internet. Some of us are charged extra if we go over so many gigabytes per month. It also helps my server from doing unnecessary processing of scripts.
I was working out a way to take care of this. PHP has a method to access the client headers individually called getallheaders(). The problem is that this only works on apache web servers. I’m testing things here at home on an Internet Information Server (iis). Although my website is hosted on apache (I think), I still would rather have support for multiple platforms.
I continued to run through some problems until I found a PHP implimation of conditional get. It was pretty easy to impliment and it worked immediately. The method expects a parameter of a date to compare. I read the date that an MP3 file was last modified and passed it in there. The method did the rest of the work telling the web browser that the information had not changed.
I didn’t realize that this term, “Conditional Get”, was coined for this pattern. The PHP implementation referred to a very good article about it called HTTP Conditional Get for RSS Hackers. It goes into detail about the whole process of it all. It’s amazing how much detail you can get into that amounts to just a few lines of code.
Tomorrow I’ll look more into caching data on the server. I found a method (md5_file()) to get a hash of a file that I can use to see if the files contents have changed since the last time I created cached xml documents and images referring to its contents. Reading all of that data from MP3 files constantly could perhaps have a toll on the servers performance that I want to avoid.