Jump to content

PHP client side caching


sdz007

Recommended Posts

Hi i am running a site which contains alot of images/css/javascript. I have added server side caching for most of my php files, currenlty i am working towards caching images and css/js on the client side to improve load times. I have come across some problems, i have written a rewrite rule in .htaccess file as follows

 

RewriteEngine On

RewriteRule ^/(.*\.)v[0-9.]+\.(css|js|gif|png|jpg)$  /client-cache.php?path=$1$2  [L] basically whnever the server encounters any of the following

 

extensions it will got my php file and that file will do the necessary caching. The contents of the php file are as follows

 

/* PHP code */

 

$period = 604800;

header("Expires: ".gmdate("D, d M Y H:i:s", time()+$period)." GMT");

header("Cache-Control: max-age=$period");

 

# ignore paths with a '..'

if (preg_match('!\.\.!', $_GET['path'])){ go_404(); }

 

# make sure our path starts with a known directory

if (!preg_match('!^(js|css|images)!', $_GET['path'])){ go_404(); }

 

# does the file exist?

if (!file_exists($_GET['path'])){ go_404(); }

 

# output a mediatype header

$ext = array_pop(explode('.', $_GET['path']));

switch ($ext){

  case 'css':

    header("Content-type: text/css");

    break;

  case 'js' :

    header("Content-type: text/javascript");

    break;

  case 'gif':

    header("Content-type: image/gif");

    break;

  case 'jpg':

    header("Content-type: image/jpg");

    break;

  case 'png':

    header("Content-type: image/png");

    break;

  default:

    header("Content-type: text/plain");

}

 

# echo the file's contents

echo implode('', file($_GET[path]));

 

function go_404(){

  header("HTTP/1.0 404 File not found");

  exit;

}

 

/*  end of code */

 

When i use live http headers in php to check if the headers are added for the files, none of the headers are added. I have added the .htaccess file in the

directory where my php source exists. I need some help on the following topic

 

hierarchy is as follows

 

/images

/classes

/css

/js

/visitor

.htaccess

client-cache.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.