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, see

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, see

This article describes how php uses gzip to compress and transfer js and css files. 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'].' http://www.jb51.net/ '; $ 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 ('.',' http://www.jb51.net/ ', $ _ GET [' B ']): $ doc_root_uri; // list of file names without the 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 ('.',' http://www.jb51.net/ ', $ _ 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.' http://www.jb51.net/ '. $ 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 anythingheader (" HTTP/1.0 304 Not Modified "); header (" Content-Type: text /". $ file_type); header ('content-Length: 0');} else {// First time visit or files were modifiedif ($ 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 assumerif (! 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 .' http://www.jb51.net/ '. $ Cachefile) {if ($ fp = fopen ($ cachedir .' http://www.jb51.net/ '. $ Cachefile, 'rb') {if ($ encoding! = 'None') {header ("Content-Encoding :". $ encoding);} header ("Content-Type: text /". $ file_type); header ("Content-Length :". filesize ($ cachedir.' http://www.jb51.net/ '. $ Cachefile); fpassthru ($ fp); fclose ($ fp); exit ;}}// Get contents of the files $ contents = ''; reset ($ elements); while (list (, $ element) = each ($ elements) {$ path = $ base.' http://www.jb51.net/ '. $ Element; $ contents. = "\ n ". file_get_contents ($ path);} // Send Content-Typeheader ("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 contentsheader ('content-Length :'. strlen ($ contents); echo $ contents;} // Store cacheif ($ cache) {if ($ fp = fopen ($ cachedir.' http://www.jb51.net/ '. $ 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.