Example of gzip transmission on the php Program Implementation page-PHP source code

Source: Internet
Author: User
When we mention gzip compression, many of our friends will think that it is very simple to enable gzip compression on the server side, such as iis, apache, and nginx, however, if you do not have server permissions, you can refer to gzip compression on the php page. When we mention gzip compression, many of our friends will think that it is very simple to enable gzip compression on the server side, such as iis, apache, and nginx, however, if you do not have server permissions, you can refer to gzip compression on the php page.

Script ec (2); script

Example

The Code is as follows:

/*
* Compressing data
*/
Public static function ob_gzip ($ content) // $ content is the page content to be compressed, or the biscuit raw material.
{
If (! Headers_sent () & // If the page header information has not been output
Extension_loaded ("zlib") & // and zlib extensions have been loaded into PHP
Strstr ($ _ SERVER ["HTTP_ACCEPT_ENCODING"], "gzip") // and the browser says it can accept GZIP pages
{
$ Content = gzencode ($ content. "OK", 9); // this page has been compressed "the annotation tag, and then compressed with the gzencode () function provided by zlib at the execution level of 9, the value range of this parameter is 0-9, 0 indicates no compression, 9 indicates the maximum compression, of course, the higher the compression level, the more expensive the CPU.

// Then use the header () function to send some header information to the browser www.111cn.net, telling the browser that the page has been compressed with GZIP!
Header ("Content-Encoding: gzip ");
Header ("Vary: Accept-Encoding ");
Header ("Content-Length:". strlen ($ content ));
}
Return $ content; // return the compressed content, or send the compressed biscuit back to the workbench.
}

Now that we have talked about php gzip, let's talk about apache.

Step 2

LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so

In httpd. add the following code to conf to add it to any blank space. If you are not familiar with apache, put it in http if you are worried about adding an error. the last line of the conf file, which can be written by a virtual server. put the htaccess file in the project.

The Code is as follows:


SetOutputFilter DEFLATE
# Don't compress images and other
SetEnvIfNoCase Request_URI .(? : Gif | jpe? G | png) $ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(? : Exe | t? Gz | zip | bz2 | sit | rar) $ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(? : Pdf | doc) $ no-gzip dont-vary
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
AddOutputFilterByType DEFLATE application/x-javascript

In this configuration, gzip compression in apache is configured. After apache is restarted, let's test it.


In the Http header:Content-Encoding: gzipGzip compression is enabled for the data returned by attribute judgment:

Use YSlow for detection. When only static File compression is started, Gzip compression is rated B:

When dynamic File compression is enabled at the same time, Gzip compression is rated as:

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.