通常ブラウザでサイトに一度アクセスすると、更新されない限りローカルのキャッシュが利用され、トラフィックを削減できる。しかし、サイトをPHP化すると動的にサイトが作成されるためにブラウザのこの機能が利用できない。ところがPHPでも条件付きGETを有効にする方法があります。詳しくはOgawa::Memoranda:「条件付きGET」のススメに書かれています。以下のコードをページの先頭に埋め込めばOK。これでネットワーク負荷を低減できます。
<?php$ts = getlastmod();doConditionalGet($ts);function doConditionalGet($timestamp) {// A PHP implementation of conditional get, see // http://fishbowl.pastiche.org/archives/001132.html$last_modified = gmdate('D, d M Y H:i:s T', $timestamp);$etag = '"'.md5($last_modified).'"';// Send the headersheader("Last-Modified: $last_modified");header("ETag: $etag");// See if the client has provided the required headers$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) :false;$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : false;if (!$if_modified_since && !$if_none_match) {return;}// At least one of the headers is there - check themif ($if_none_match && $if_none_match != $etag) {return; // etag is there but doesn't match}if ($if_modified_since && $if_modified_since != $last_modified) {return; // if-modified-since is there but doesn't match}// Nothing has changed since their last request - serve a 304 and exitheader('HTTP/1.1 304 Not Modified');exit;}?>サーバーのアクセスログをみると「HTTP/1.1" 200」から「HTTP/1.1" 304」に変わったことがわかりサーバー負荷が低減されていることを実感できます。