Two ways to turn on gzip compression in PHP Web programs

Source: Internet
Author: User
Tags gz file
Gzip is a kind of network data compression transmission technique, can let our bandwidth save about 70%, can greatly speed up the page opening speed, not only benefit the user experience, also can save a lot of bandwidth. General Apache, Nginx and so on in the configuration file can open the Gzip service. Let's talk about how to turn on the gzip compression service in the PHP Web program:

The first type of gzip compression scheme

1. Add the following code to the. htaccess:


#这是添加你要压缩的类型



2, gzip.php, the code is as follows:







break; Scutephp.com








Break


?>

The second type of gzip compression scheme

This option is an upgraded version of Gzip that caches compressed files of gzip and avoids duplication of compression,

1. Add in. htaccess:

Htaccess rewriterule (. *.css$|. *.js$) gzip.php?$1 [L],

The code is as follows:

Htaccess

Rewriterule (. *.css$|. *.js$) gzip.php?$1 [L]//Project Root Path

Define (' Abspath ', DirName (__file__). ' /'); GZIP compression switch

$cache = true; directory where GZ files are stored, ensuring writable

$cachedir = ' gzip-cache/';

if (!is_dir (Abspath. $cachedir)) {

mkdir (Abspath. $cachedir); }//Determine if gzip support is supported

$gzip = strstr ($_server[' http_accept_encoding '), ' gzip ');

$deflate = strstr ($_server[' http_accept_encoding '), ' deflate '); See if the browser supports gzip otherwise it's deflate and none

$encoding = $gzip? ' gzip ': ($deflate? ' Deflate ': ' None ');

if (!isset ($_server[' query_string ')) exit ();

$key =array_shift (Explode ('? ', $_server[' query_string '));

$key =str_replace ('.. /', ", $key);

$key =basename ($_server[' query_string '); Absolute path to File

$filename =abspath.$_server[' query_string '];

$symbol = ' ^ '; $rel _path=str_replace (Abspath, ", DirName ($filename));

$namespace =str_replace ('/', $symbol, $rel _path);

$cache _filename=abspath. $cachedir. $namespace. $symbol. basename ($filename). GZ ';//Generate GZ file path

$type = "content-type:text/html"; The default type information

$pathInfo = PathInfo ($filename); Determine file type information by suffix

$ext = $pathInfo [' extension '];

Switch ($ext) {

Case ' CSS ':

$type = "Content-type:text/css"; Break Case ' JS ':

$type = "Content-type:text/javascript";

Break

Default

Exit ();

}

if ($cache) {

if (file_exists ($cache _filename)) {//If there is a GZ file

$mtime = Filemtime ($cache _filename);

$gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT '; Read GZ file output

$content = file_get_contents ($cache _filename);

Header ("last-modified:".) $gmt _mtime);

Header ("Expires:"); Header ("Cache-control:");

Header ("Pragma:");

Header ($type);

Header ("Tips:normal Respond (Gzip)");

Header ("Content-encoding:gzip");

Echo $content;

}else if (file_exists ($filename)) {//does not have a corresponding GZ file

$mtime = Mktime ();

$gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT ';

$content = file_get_contents ($filename);//Read file

$content = Gzencode ($content, 9, $gzip? force_gzip:force_deflate);//Compress file contents

Header ("last-modified:".) $gmt _mtime);

Header ("Expires:");

Header ("Cache-control:");

Header ("Pragma:");

Header ($type);

Header ("Tips:build gzip File (gzip)"); Header ("content-encoding:".) $encoding);

Header (' Content-length: '. strlen ($content)); if ($fp = fopen ($cache _filename, ' W ')) {//write GZ file for next use

Fwrite ($fp, $content); Fclose ($FP);

}

Echo $content;

}else{

Header ("http/1.0 404 Not Found");

}

}else {//processing does not use the output in gzip mode. The basic principle of the above if (File_exists ($filename)) {

$mtime = Filemtime ($filename);

$gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT ';

Header ("last-modified:".) $gmt _mtime);

Header ("Expires:");

Header ("Cache-control:");

Header ("Pragma:");

Header ($type);

Header ("Tips:normal Respond");

$content = ReadFile ($filename);

Echo $content;

}else {

Header ("http/1.0 404 Not Found");

}

}

?>

  • 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.