Codesnipp.it Social Code Sharing

John Clarke

php : load and compress CSS dynamically

by John Clarke on Jun 16, 2011

<?php /** * Dynamic CSS loader and compressor * @author JA Clarke * @since 2011/06/17 */ // Enable GZip if supported if( !ob_start('ob_gzhandler') ) ob_start(); header('Content-Type: text/css; charset: UTF-8'); header('Cache-Control: must-revalidate'); // Note that this is PHP ver.5.2 up $offset = date_offset_get(new DateTime); header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $offset) . ' GMT'); define( 'DS', '/' ); // Just specify the location of the CSS files here define( 'CSS_PATH', dirname( __FILE__ ) . DS . 'resources' . DS . 'css' . DS ); // MAGIC TIME! ^^ $params = array_merge( $_REQUEST, $_GET, $_POST ); // I like playing it safe :) if( isset($params['file']) && !empty($params['file']) ) : // Just a check that a file(s) was requested $files = explode('|', $params['file']); // Supports multiple file requests in single entry :) $css_content = array(); foreach( $files as $file ) : if( file_exists(CSS_PATH . $file . '.css') ) : // Failsafe $css_content[] = file_get_contents(CSS_PATH . $file . '.css'); endif; endforeach; $css_content = implode( PHP_EOL, $css_content ); if( !empty($css_content) ) : echo $css_content; ob_flush(); flush(); endif; endif; /* End of File: css.php */

Can't see the comments? Please login first :)