PHP script to join multiple javascript files into one file, compress with gzip, set expiration headers

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    script PHP script to join multiple javascript files into one file, compress with gzip, set expiration headers

    Speed Up your sites with the script below.

    Join multiple javascript files into one file, compress them with gzip and set expiration header.

    replace:
    PHP Code:
    include('./js/file1.js');
    include(
    './js/file2.js');
    include(
    './js/file3.js'); 
    with your js files

    PHP Code:
    <?php
    $offset 
    = 60 * 60 * 24 * 30; // Cache for a day
    header("Content-type: text/javascript");
    header ("Cache-Control: max-age=" . $offset . ", must-revalidate");
    header ("Expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT");
    function 
    compress($buffer) {
    /* remove comments and empty lines */
    //$buffer = preg_replace('!/\*.*?\*/!s', '', $buffer);
    $buffer = preg_replace('(// .+)', '', $buffer);
    $buffer = str_replace(array("//\r\n", "//\n"), '', $buffer);
    $buffer = preg_replace('/\n\s*\n/', "\n", $buffer);
    return 
    $buffer;
    }
    ob_start();
    include(
    './js/file1.js');
    include(
    './js/file2.js');
    include(
    './js/file3.js');
    $out = ob_get_contents();
    ob_end_clean();
    $out = compress($out);
    if(
    strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip"))
    {
    header("Content-Encoding: gzip");
    header("Vary: Accept-Encoding");
    ob_start('ob_gzhandler');
    }
    echo 
    $out;
    ?>
    Advertise your mobile site for FREE with AdTwirl

Working...
X