Skip to content

Instantly share code, notes, and snippets.

@timbenniks
Created May 19, 2016 19:53
Show Gist options
  • Save timbenniks/9cf7452b4075fac0ee2176f52e139633 to your computer and use it in GitHub Desktop.
Save timbenniks/9cf7452b4075fac0ee2176f52e139633 to your computer and use it in GitHub Desktop.
Script to render static resources through PHP
<?php
$file = $_GET[ 'file' ];
$ext = $_GET[ 'ext' ];
$filename = $file.'.'.$ext;
header( 'Date: ' . gmdate( 'D, d M Y H:i:s GMT' ) );
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', strtotime( '+ 10 years' ) ) . ' GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', filemtime( $filename ) ) . ' GMT' );
header( 'Cache-Control: public, max-age=315360000' );
header( 'Pragma: ' );
if( $ext == 'css' ){
header( 'Content-type: text/css; charset=utf-8' );
}
elseif( $ext == 'js' ){
header( 'Content-type: application/x-javascript; charset=utf-8' );
}
else {
header( 'Content-type: text/html; charset=utf-8' );
}
function make_gzip( $source ){
if( !headers_sent() && extension_loaded( "zlib" ) && strstr( $_SERVER[ "HTTP_ACCEPT_ENCODING" ], "gzip" ) ){
$source = gzencode( $source, 9 );
header( "Content-Encoding: gzip" );
header( "Vary: Accept-Encoding" );
header( "Content-Length: ".strlen( $source ) );
return $source;
}
}
if( file_exists( $file.'.'.$ext ) ){
echo make_gzip( file_get_contents( $file.'.'.$ext ) );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment