Php controls page browser cache and enables gzip Transfer

Source: Internet
Author: User
Tags metabase

This article will introduce you to the php control page browser cache and the gzip transfer PAGE method. I hope this code will be helpful to you, if you do not have the server permission, you can transfer the gzip page of the server.

Php instance gzip transfer page

The Code is as follows: Copy code

It can be used for static resources and html pages. Modifications are also applicable to other languages.

// Cache expiration time (Cache-Control, http1.1), recommended
$ Secs = 0; // second
Header ("Cache-Control: s-maxage = $ secs, max-age = $ secs, must-revalidate, proxy-revalidate ");

// The cache expiration time is 24 hours (Expires)
$ Offset = 3600*24;
$ Expire = "Expires:". gmdate ("D, d m y h: I: s", time () + $ offset). "GMT ";
Header ($ expire );

// Last-Modified, Last Modified Time
$ Gmt_mtime = gmdate ('d, d m y h: I: s', time (). 'gmt ';
Header ("Last-Modified:". $ gmt_mtime );

// Enable gzip Compression
Ob_start ();
... Your php code...
... Your php code...
... Your php code...
Header ('content-Length: '. ob_get_length ());
Ob_end_flush ();

If you have server permissions, refer to apache to enable gzip transfer.


1. Find your httpd. conf file and open the file as follows:

LoadModule deflate_module modules/mod_deflate.so

Go to the previous #, because the mod_deflate.so module is disabled by default.

2. Put the current code at the end of your httpd. conf file.

The Code is as follows: Copy code

<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
DeflateCompressionLevel 5
AddOutputFilterByType DEFLATE text/html text/css image/gif image/jpeg image/png application/x-javascript
</IfModule>

Restart apache and then KO.

Enable gzip page transmission in iis

1. Open the Internet Information Service (IIS) manager, right-click "website"-"" properties ", and select" service ". In the "HTTP compression" box, select "compressing Application Files" and "compressing static files", and set "temporary directory" and "maximum temporary directory limit" as needed ";

2. In the Internet Information Service (IIS) manager, right-click "Web Service extension"-"Add a new Web Service extension ...", Enter the extension "HTTP Compression" in the "New Web service extension" box and add the "required file" as C: WINDOWSsystem32inetsrvgzip. dll, the Windows System directory may vary depending on your installation, select "set extension status to allow ";

3. Use a text editor to open C: WindowsSystem32inetsrvMetaBase. xml (backup is recommended first ),

Locate Location = "/LM/W3SVC/Filters/Compression/gzip to set gzip Compression,

Locate Location = "/LM/W3SVC/Filters/Compression/deflate" to set deflate Compression. The above two nodes are next to each other. And set the same attributes.

To Compress dynamic files, set HcDoDynamicCompression to "TRUE",

Add the dynamic file suffix you want to compress in HcScriptFileExtensions, such as aspx;

To Compress static files, set HcDoStaticCompression and HcDoOnDemandCompression to "TRUE",

Add the extension name of the static file to be compressed in HcFileExtensions, such as xml and css;

HcDynamicCompressionLevel and HcOnDemandCompLevel indicate the required compression ratio. The value ranges from 0 to 10. The default value is 0.

HcDynamicCompressionLevel attribute description: HcDynamicCompressionLevel Metabase Property

HcOnDemandCompLevel attribute description: HcOnDemandCompLevel Metabase Property

Note: These two attribute values are generally recommended to be set to 9, with the best cost effectiveness.

Note:

1. stop IIS before editing MetaBase. xml. You can use net stop iisadmin

2. After the modification is complete, enable the iisadmin service and execute the iisreset command.

SEO Testing

Whether compression has an impact on SEO. After testing, it can still be well recorded after being enabled.

For more information about the SEO impact, see the following articles.

Specific efficiency issues will be monitored for further research

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.