SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist Mr. Tech's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    562
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can you check my php caching script?

    I found some code on the net to cache php files. I've modified it to suit.

    Does the following code look ok to you?
    Is it going to be valid in all browsers?
    Any other headers I need to add?

    PHP Code:
    // Settings
    $cachedir ABSPATH 'cache/'// Directory to cache files in
    $cachetime 600// Seconds to cache files for
    $cacheext 'html.gz'// Extension to give cached files
     
    // Ignore List
    $ignore_list = array();
     
    // Script
    $page 'http://' $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // Requested page
    $cachefile $cachedir md5($page) . '.' $cacheext// Cache file to either load or create
     
    $ignore_page false;
    for (
    $i 0$i count($ignore_list); $i++) {
        
    $ignore_page = (strpos($page$ignore_list[$i]) !== false) ? true $ignore_page;
    }
     
    $cachefile_created = ((@file_exists($cachefile)) and ($ignore_page === false)) ? @filemtime($cachefile) : 0;
    @
    clearstatcache();
     
    // Show file from cache if still valid
    if (time() - $cachetime $cachefile_created) { 
        
    header("Last-Modified: " gmdate("D, d M Y H:i:s \G\M\T"$cachefile_created));
        
    header("Content-Encoding: gzip");
        
    header("Vary: Accept-Encoding");
        
    header("Cache-Control: private, must-revalidate, s-max-age=0");
        @
    readfile($cachefile);
        exit(); 
    }

    // If we're still here, we need to generate a cache file 
    ob_start();

    /*************
    *
    *
    *
    * PAGE CONTENT IS LOADED HERE
    *
    *
    *
    *************/

    // Now the script has run, generate a new cache file
    $fp = @fopen($cachefile'w');

    // gzip content
    $cached_content ob_get_contents();
    $cached_content_gzip gzencode($cached_content6);

    // save the contents of output buffer to the file
    @fwrite($fp$cached_content_gzip);
    @
    fclose($fp);

    ob_end_flush(); 

  2. #2
    SitePoint Enthusiast jwh335's Avatar
    Join Date
    Dec 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm just lurking around to pick up some knowledge about PHP, so take what I say with a grain of salt. However, I don't think there is such thing as 'valid' PHP. There is PHP that works and PHP that doesn't work. The browser isn't going to render the PHP, just send the commands to the server.

    Also, is it not working?

  3. #3
    SitePoint Evangelist Mr. Tech's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    562
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea it's working. I just wanted to see if there was a more efficient way to do it. Plus I wanted to make sure the headers I was submitting through would be compatible with all browsers. Browsers use the headers even though they are submitted via PHP

  4. #4
    SitePoint Enthusiast jwh335's Avatar
    Join Date
    Dec 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would it make it more efficient if you were to comment out:

    $ignore_list = array();

    and

    for ($i = 0; $i < count($ignore_list); $i++) {
    $ignore_page = (strpos($page, $ignore_list[$i]) !== false) ? true : $ignore_page;
    }

    It seems like the for loop isn't going to run because the array is empty. So, you'd save the server some time on initializing a variable and on checking the first condition in the loop. I suppose that's a little nit picky, though :P

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This assumes the browser accepts gzip encoding. If it doesn't, or does but didn't ask for it, it will break horribly.

    clearstatcache() is in the wrong place.

    the cache-control: directive could be a bit more lenient by specifying how long. Otherwise the browser may not cache at all.

  6. #6
    SitePoint Evangelist Mr. Tech's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    562
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that crmalibu!
    Where should the clearstatcache() function be placed?
    Should max-age in the cache-control header be something like 300?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •