How php uses gzip to compress and transmit js and css files

Source: Internet
Author: User
This article describes how to use gzip to compress and transfer js and css files in php. The example shows how to use gzip to compress js and css files, for more information about how php uses gzip to compress JavaScript and css files, see the following example. Share it with you for your reference. The details are as follows:

<? Php/*** complete call example: * 1. combine. php? T = j & B = public & fs = jslib. jquery, function ** this example calls the public/jslib/jquery under the root directory of the website. js and public/function. js ** 2. combine. php? T = j & fs = jslib. jquery, function ** this example calls jslib/jquery. js and function. js ** in the root directory of the website. 3. combine. php? Tsf-c& B ;public.css & fs = common, index ** this example calls public/css/common.css and public/css/index.css under the root directory of the website ** 4. combine. php? T = c & fs = css. common * this example calls css/common.css under the root directory of the website. ** Note: Multiple file names are separated by commas (,). only one file name is not available at the end, multiple separated files will be compressed into one file and sent to the browser at a time **/$ is_bad_request = false; $ cache = true; $ doc_root_uri = $ _ SERVER ['document _ root']. '/'; $ cachedir = $ doc_root_uri. 'Public/cache'; // file type. The value j indicates js, and the value c indicates css $ type = isset ($ _ GET ['t']). ($ _ GET ['t'] = 'J' | $ _ GET ['t'] = 'C '? $ _ GET ['t']: ''):''; // The Base Directory for storing js and css files, for example :? B = public. js represents the/public/js folder, starting from the website Root Directory // The Base Directory parameter is not required, if there is a base directory, this base directory will be appended before the file name $ base = isset ($ _ GET ['B'])? ($ Doc_root_uri.str_replace ('. ','/', $ _ GET [' B ']): $ doc_root_uri; // list of file names without a suffix. for example, the format of the base directory is // File name: Base Directory (if any) + file package name + file name // for example, the type is j, // The file name is public. js. jquery // if the base path is public, // The converted file name is/public/js/jquery. js // if there is no base path //, the converted file name is/public/js/jquery. js // multiple file names are separated by commas (,) $ fs = isset ($ _ GET ['Fs'])? Str_replace ('. ','/', $ _ GET ['Fs']): ''; $ fs = str_replace (',','. '. ($ type = 'J '? 'JS, ': 'css,'), $ fs); $ fs = $ fs. ($ type = 'J '? '. Js': '.css '); if ($ type = ''| $ fs ='') {$ is_bad_request = true;} // die ($ base ); if ($ is_bad_request) {header ("HTTP/1.0 503 Not Implemented");} $ file_type = $ type = 'J '? 'Javascript ': 'css'; $ elements = explode (',', preg_replace ('/([^?] *). */',' \ 1', $ fs); // Determine last modification date of the files $ lastmodified = 0; while (list (, $ element) = each ($ elements) {$ path = $ base. '/'. $ element; if ($ type = 'J' & substr ($ path,-3 )! = '. Js') | ($ type = 'C' & substr ($ path,-4 )! = '.Css ') {header ("HTTP/1.0 403 Forbidden"); exit;} if (substr ($ path, 0, strlen ($ base ))! = $ Base |! File_exists ($ path) {header ("HTTP/1.0 404 Not Found"); exit;} $ lastmodified = max ($ lastmodified, filemtime ($ path ));} // Send Etag hash $ hash = $ lastmodified. '-'. md5 ($ fs); header ("Etag :\"". $ hash. "\" "); if (isset ($ _ SERVER ['http _ IF_NONE_MATCH ']) & stripslashes ($ _ SERVER ['http _ IF_NONE_MATCH']) = '"'. $ hash. '"') {// Return visit and no modifications, so do not send anything header (" H TTP/1.0 304 Not Modified "); header (" Content-Type: text /". $ file_type); header ('content-Length: 0');} else {// First time visit or files were modified if ($ cache) {// Determine supported compression method $ gzip = strstr ($ _ SERVER ['http _ ACCEPT_ENCODING '], 'gzip '); $ deflate = strstr ($ _ SERVER ['http _ ACCEPT_ENCODING '], 'release'); // Determine used compression method $ encoding = $ gzip? 'Gzip': ($ deflate? 'Release': 'none'); // Check for buggy versions of Internet Explorer if (! Strstr ($ _ SERVER ['http _ USER_AGENT '], 'Opera') & preg_match ('/^ Mozilla \/4 \. 0 \ (compatible; MSIE ([0-9] \. [0-9])/I ', $ _ SERVER ['http _ USER_AGENT'], $ matches) {$ version = floatval ($ matches [1]); if ($ version <6) $ encoding = 'none'; if ($ version = 6 &&! Strstr ($ _ SERVER ['http _ USER_AGENT '], 'ev1') $ encoding = 'none ';} // Try the cache first to see if the combined files were already generated $ cachefile = 'cache -'. $ hash. '. '. $ file_type. ($ encoding! = 'None '? '. '. $ Encoding: ''); if (file_exists ($ cachedir. '/'. $ cachefile) {if ($ fp = fopen ($ cachedir. '/'. $ cachefile, 'RB') {if ($ encoding! = 'None') {header ("Content-Encoding :". $ encoding);} header ("Content-Type: text /". $ file_type); header ("Content-Length :". filesize ($ cachedir. '/'. $ cachefile); fpassthru ($ fp); fclose ($ fp); exit ;}}// Get contents of the files $ contents = ''; reset ($ elements); while (list (, $ element) = each ($ elements) {$ path = $ base. '/'. $ element; $ contents. = "\ n ". file_get_contents ($ path );} // Send Content-Type header ("Content-Type: text/". $ file_type); if (isset ($ encoding) & $ encoding! = 'None') {// Send compressed contents $ contents = gzencode ($ contents, 9, $ gzip? FORCE_GZIP: FORCE_DEFLATE); header ("Content-Encoding :". $ encoding); header ('content-Length :'. strlen ($ contents); echo $ contents;} else {// Send regular contents header ('content-Length :'. strlen ($ contents); echo $ contents;} // Store cache if ($ cache) {if ($ fp = fopen ($ cachedir. '/'. $ cachefile, 'WB ') {fwrite ($ fp, $ contents); fclose ($ fp );}}}

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.