Jazajay
Active Member
Right I want to start off by saying that I have not written any code yet but will do hopefully later today when I get up. Now I currently cache my CSS files for like 10 years into the future saves on repeat users having to download them next time and this reduces http headers and thus speeds the page download up. Now you run into a problem and that is if you make amendments to that file anyone who viewed that file before will still see the old copy.
Now the way I get around this currently is with a technique known as file versioning, that is you make 1 amendment and you change the file name, this tricks the browser into thinking it is a new file, because well it is, and the new updates are then added. However as with everything I reckon I can do a better job.
So here's my theory/problem and I'll post my code written off the cuff it may not work but I don't see why it wont. I will warn you now it will get geeky.
So I was adding a hash to a program I have recently written and it occurred to me the best thing about a hash is if 1 character is different the hash will be changed. I guess some of you see where I am going with this.
So we get the CSS file and hash it, we then set a cookie of the hash on the users computer. We then test for the cookie and get the hash from it. If the hash matches the CSS it hasn't changed if it doesn't or if the cookie is not present, incase the user wiped their cookies, we add the cache control PHP header telling it to revalidate it's copy thus no worry of the user having an out of date sheet, I don't have to spend time changing and uploading multiple files then.
So off the top of my head something like this.
1. <?php
2. $hash=hash_file('md5', main.css');
3. if(!isset($_COOKIE['cache']) || $hash!==$_COOKIE['cache']){
4. setcookie("cache",$hash,time()+315360000)?>
5. <link type="text/css" href="css/main.css" />
6. <?php }?>
Now how to get it to revalidate is currently where my theory gets a tad unstuck as I'm not sure about this bit.
Mmm...okay what about wacking in a bit of mod_rewrite and getting a tad clever.
So we have the above code.
We then write a .htaccess rewrite rule changing the file but by adding a get variable that we don't do anything with we then save that in the cookie for future use.
So ~
1. <?php
2. $hash=hash_file('md5', main.css');
3. if(!isset($_COOKIE['cache']) || $hash!==$_COOKIE['cache']){
4. setcookie("cache",$hash,time()+315360000);
5. if(isset($_COOKIE['num']))
6. {
7. $newNum=$_COOKIE['num']++;
8. }else{
9. $newNum=0;
10. }?>
11. <link type="text/css" href="css/main.<?php echo $newNum;?>.css" />
12. <?php setcookie("num",$newNum,time()+315360000);
13. }else{?>
14. <link type="text/css" href="css/main.<?php echo $_COOKIE['num'];?>.css" />
15. <?php }?>
Then with a bit of mod rewrite ~
RewriteEngine On
RewriteRule ^css/main.(.*).css$ css/main.css.php?num=1
Then in the CSS sheet which would now be a PHP file we tell it to parse it as a CSS file so at the top of the file we write ~
<?php header ("content-type: text/css; charset: UTF-8")?>
And that should then request a "new" file which is in fact the same file with a get variable added to it but the updates should then take effect.
So what do ppl think?
Bare in mind this is just a theory at the mo and I haven't tested my code yet.
Any improvements, anything I could do better, wont it work, if not why not?
Pretty much feedback wanted.
Jaz
Key:
Red ~ PHP
Blue ~ XHTML
Brown ~ Mod_Rewrite
Now the way I get around this currently is with a technique known as file versioning, that is you make 1 amendment and you change the file name, this tricks the browser into thinking it is a new file, because well it is, and the new updates are then added. However as with everything I reckon I can do a better job.
So here's my theory/problem and I'll post my code written off the cuff it may not work but I don't see why it wont. I will warn you now it will get geeky.
So I was adding a hash to a program I have recently written and it occurred to me the best thing about a hash is if 1 character is different the hash will be changed. I guess some of you see where I am going with this.
So we get the CSS file and hash it, we then set a cookie of the hash on the users computer. We then test for the cookie and get the hash from it. If the hash matches the CSS it hasn't changed if it doesn't or if the cookie is not present, incase the user wiped their cookies, we add the cache control PHP header telling it to revalidate it's copy thus no worry of the user having an out of date sheet, I don't have to spend time changing and uploading multiple files then.
So off the top of my head something like this.
1. <?php
2. $hash=hash_file('md5', main.css');
3. if(!isset($_COOKIE['cache']) || $hash!==$_COOKIE['cache']){
4. setcookie("cache",$hash,time()+315360000)?>
5. <link type="text/css" href="css/main.css" />
6. <?php }?>
Now how to get it to revalidate is currently where my theory gets a tad unstuck as I'm not sure about this bit.
Mmm...okay what about wacking in a bit of mod_rewrite and getting a tad clever.
So we have the above code.
We then write a .htaccess rewrite rule changing the file but by adding a get variable that we don't do anything with we then save that in the cookie for future use.
So ~
1. <?php
2. $hash=hash_file('md5', main.css');
3. if(!isset($_COOKIE['cache']) || $hash!==$_COOKIE['cache']){
4. setcookie("cache",$hash,time()+315360000);
5. if(isset($_COOKIE['num']))
6. {
7. $newNum=$_COOKIE['num']++;
8. }else{
9. $newNum=0;
10. }?>
11. <link type="text/css" href="css/main.<?php echo $newNum;?>.css" />
12. <?php setcookie("num",$newNum,time()+315360000);
13. }else{?>
14. <link type="text/css" href="css/main.<?php echo $_COOKIE['num'];?>.css" />
15. <?php }?>
Then with a bit of mod rewrite ~
RewriteEngine On
RewriteRule ^css/main.(.*).css$ css/main.css.php?num=1
Then in the CSS sheet which would now be a PHP file we tell it to parse it as a CSS file so at the top of the file we write ~
<?php header ("content-type: text/css; charset: UTF-8")?>
And that should then request a "new" file which is in fact the same file with a get variable added to it but the updates should then take effect.
So what do ppl think?
Bare in mind this is just a theory at the mo and I haven't tested my code yet.
Any improvements, anything I could do better, wont it work, if not why not?
Pretty much feedback wanted.
Jaz
Key:
Red ~ PHP
Blue ~ XHTML
Brown ~ Mod_Rewrite