css - How can I manage browser cache in PHP? -


my idea simple, take css files , generate 1 minified in time of change in css file. tell browser clear cache. if there unchanged file in browser cache use - user don't need redownload every time.

i'm using following snip of code that. part using cache bit buggy, of time works tell browser use cached version (as there no change) , browser using old 1 , user must client side cache refresh.

could give me advice how that, refresh client side browser cache everytime when change occurs , if there no change use cache?

$cssfiles = getcssfiles();  $fm = new fileminifier(fileminifier::type_css); $lastmodified = $fm->lastmodification($cssfiles); $savedlastmodified = dateutils::converttotimestamp($this->system->systemsettings['csslastchange']);  $etagfile = md5('css-file'); header("content-type: text/css"); header("pragma: public"); header('cache-control: public'); header("last-modified: " . gmdate("d, d m y h:i:s", $lastmodified) . " gmt"); header("etag: $etagfile");  // if there change - generate new minified css if ($lastmodified > $savedlastmodified) {     // take files minify them, save , redirect output, update last change time     ... } // or use generated else {     $ifmodifiedsince = (isset($_server['http_if_modified_since']) ? $_server['http_if_modified_since'] : 0);     $etagheader = (isset($_server['http_if_none_match']) ? trim($_server['http_if_none_match']) : false);     // if in chache use it! - no need redownloading     if (strtotime($ifmodifiedsince) == $lastmodified || $etagheader == $etagfile)     {         header("http/1.1 304 not modified");         exit;     }     $this->data['text'] = file_get_contents(systeminfo::getserverroot() . '/public/css/minified.css'); } 

what you're trying admirable, it's bit of re-inventing wheel. far css files, javascript files, etc concerned, modern browsers fine job of pulling unchanged files cache.

manipulating http headers notify browser of file change do-able, there browser differences (especially older browsers) in how headers interpreted makes approach fraught nuance.

it far easier accomplish goal versioning css includes. change in file version prompt browser re-download file.

example:

before file change:

<link href="http://yourwebsite.com/file.css?_=1.0.0.1" rel="stylesheet" type="text/css"> 

after file change:

<link href="http://yourwebsite.com/file.css?_=1.0.0.2" rel="stylesheet" type="text/css"> 

all browsers interpret change in uri parameter new file , re-download it.

it's possible automate versioning don't need manually edit include line after every change. here's 1 way it...

example:

<?php     $ver = filemtime($filename);     echo '<link href="http://yourwebsite.com/file.css?_='.$ver.'" rel="stylesheet" type="text/css">'; ?> 

that code place append modified date of file (unix timestamp format) uri of file include.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -