Php checks whether the url page is compressed using gzip.

Source: Internet
Author: User
Gzip compression is a good way to compress and transfer pages. we often open it on the server, so that a KB file may be less than 20 kB after being compressed by gzip, next I will introduce the gzip compression detection and enabling methods. how to Open gzip... gzip compression is a good way to compress and transfer pages. we often open it on the server, so that a KB file may be less than 20 kB after being compressed by gzip, next I will introduce the gzip compression detection and enabling methods.

How to enable gzip compression?

Step 2 of apache, the code is as follows:

LoadModule deflate_module modules/mod_deflate.so

LoadModule headers_module modules/mod_headers.so

Add the following code in http. conf:

# Begin gzip # enable gzip compression for apache. you must install the gzip module.
 
  
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/x-javascript application/javascript
 # END GZIP

If you are an nginx server, refer to the following code:

gzip on;  gzip_min_length 1000;  gzip_buffers 4 8k;  gzip_http_version 1.1;  gzip_types text/text text/html text/plain text/xml  text/css application/x-javascript application/javascript;

Restart apache or nginx. how can I check whether gzip is enabled correctly? the following php code can be used:

// Use gzip to compress the webpage. // file_get_contents directly obtains the webpage garbled characters. Header ('content-Type: text/html; charset = utf-8 '); $ url = 'http: // www.phprm.com'; $ file = fopen ($ url, "rb"); // read-only 2 bytes. if the value is (hexadecimal) 1f 8b (10 hexadecimal) 31 139, gzip is enabled; $ bin = fread ($ file, 2); fclose ($ file); $ strInfo = @ unpack ("C2chars", $ bin); $ typeCode = intval ($ strInfo ['chars1']. $ strInfo ['chars2 ']); $ isGzip = 0; switch ($ typeCode) {case 31139: // The website has enabled gzip $ isGzip = 1; break; default: $ isGzip = 0;} $ url = $ is Gzip? "Compress. zlib ://". $ url: $ url; // ternary expression $ mierHtml = file_get_contents ($ url); // Obtain Mill military network data $ mierHtml = iconv ("gbk", "UTF-8 ", $ mierHtml); echo $ mierHtml;

Example 2 code:

 


Article URL:

Reprint ^ at will, but please attach the tutorial address.

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.