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.

What if gzip compression is enabled?

Apache

Step 2

The Code is as follows: Copy code

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

Add the following code in http. conf:

The Code is as follows: Copy code

# BEGIN GZIP
# Before enabling gzip compression for apache, install the gzip module.
<Ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html
Text/plain text/xml text/css application/x-javascript
Application/javascript
</Ifmodule>
# END GZIP

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

The Code is as follows: Copy code

# Enable gzip compression in nginx and put it in location
Gzip on;
Gzip_min_length 1000;
Gzip_buffers 4 8 k;
Gzip_http_version 1.1;
Gzip_types 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.

 

The Code is as follows: Copy code

// Mill military network uses gzip to compress webpages
// The webpage directly obtained by file_get_contents is garbled.
Header ('content-Type: text/html; charset = UTF-8 ');
$ Url = 'HTTP: // www. bKjia. c0m ';
$ File = fopen ($ url, "rb ");
// Read-only 2 bytes. If the value is (in hexadecimal format) 1f 8b (in hexadecimal format) 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 = $ isGzip? "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

The Code is as follows: Copy code

<? Php
/*
Php determines whether the url page is compressed using gzip
*/
$ Ch = curl_init ("http://www.hzhuti.com/"); // url cannot have redirection
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, array ('Accept-Encoding: gzip, deflate '));
Curl_setopt ($ ch, CURLOPT_HEADER, 1 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
$ Buffer = curl_exec ($ ch );
$ Curl_info = curl_getinfo ($ ch );
Curl_close ($ ch );
$ Header_size = $ curl_info ["header_size"];
// Header Length
$ Headers = substr ($ buffer, 0, $ header_size );
// Obtain the header information
$ Body = substr ($ buffer, $ header_size );
// Obtain the webpage content

Function getEncoding (& $ headers ){
$ Arr = explode ("rn", trim ($ headers ));
Array_shift ($ arr );
Foreach ($ arr as $ header ){
List ($ k, $ v) = explode (':', $ header );
If ('content-encoding' = strtolower ($ k )){
Return trim ($ v );
}
}
Return false;
}

$ Encoding = getEncoding ($ headers );

If ($ encoding ){
Echo "Using:". $ encoding;
} Else {
Echo "None ";
}

?>
 

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.