PHP caching woes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CompressedAir
    Junior Member
    • Apr 2004
    • 4

    #1

    PHP caching woes

    After finally developing some efficient cache-friendly coding for my site on my local server, it seems that it doesn't work at all when I upload it to my server here. Here is a sample of a css file that I've made.
    PHP Code:
    <?
    header( "Content-Type: text/css" );
    ob_start();
    ob_start("ob_gzhandler");
    ?>
    body {
    color: black;
    margin: 0;
    etc. etc.
    }
    <?

    ob_end_flush(); //gzip end_flush

    @clearstatcache();
    $mtime=filemtime($_SERVER["SCRIPT_FILENAME"]);
    $gmt_mtime = gmdate("D, d M Y H:i:s",$mtime) . " GMT";
    $size=ob_get_length();
    $cache_etag="\"".md5($size)."\"";

    header("Last-Modified: $gmt_mtime");
    header("Content-Length: $size");
    header("ETag: $cache_etag");

    if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
            if ($_SERVER['HTTP_IF_NONE_MATCH'] == $cache_etag ) {
                header("HTTP/1.1 304 Not Modified");
                ob_end_clean();
                exit();
                }
        }

    else if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
       if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $gmt_mtime) {
           header("HTTP/1.1 304 Not Modified");
                 ob_end_clean();
                 exit();
       }
    }

    /*echo $_SERVER['HTTP_IF_NONE_MATCH'] ."http";
    echo $_SERVER['HTTP_IF_MODIFIED_SINCE']. "http";
    echo $cache_etag. "cache etag";
    echo $gmt_mtime. "mtime";*/

    ob_end_flush();
    ?>
    Now this script should check if the last modified headers are the same or the generated etag are the same as the requested ones and send out the proper 304 response. This method seems to work on my php 4.3.6/apache 2.0.49 server, however, when I test it here, and check my logs, apache is always sending out 200 http ok respoonse instead of 304. I checked to make sure it sent 304 responses by just doing "if (1==1) header("HTTP/1.1 304 Not Modified");" which worked.
    I've tried echoing the values, which is only possible when I comment out the first ob_end_flush and they seem to be correct. Maybe I am not doing this buffering thing correctly. I'm new to using buffers.
    So I'm really at a loss now. Is there a problem with my script?

    Thanks!
  • Buddha
    Senior Member
    • Mar 2004
    • 825

    #2
    You might try getting that content-type header in a buffer.

    and ...

    Why two buffers, I think (not a good thing for me to be doing this earily on a Sunday) you only need one since headers aren't sent to the callback.

    PHP Code:
    ob_start("ob_gzhandler");
    header"Content-Type: text/css" ); 
    As why it ain't working, well it must be too earily cause it looks like it should. You might check to make sure all those HTTP_ constants are availiable on the Dathorn server?
    "Whatcha mean I shouldn't be rude to my clients?! If you want polite then there will be a substantial fee increase." - Buddha

    Comment

    • CompressedAir
      Junior Member
      • Apr 2004
      • 4

      #3
      Hmm well after some more wrestling I just uploaded by local servers php.ini and gave it a whirl. Surprise! Works as normal now I guess something in there needed to be added.
      All working now

      Comment

      Working...