Php controls page browser cache and enables gzip transfer

Source: Internet
Author: User
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 and those who do not have Server permissions can.

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.

The gzip transfer page of the php instance can be used for static resources and html pages. the modification is also applicable to other languages. the code is as follows:

  1. // Cache Expiration Time (Cache-Control, http1.1), recommended
  2. $ Secs = 0; // second
  3. Header ("Cache-Control: s-maxage = $ secs, max-age = $ secs, must-revalidate, proxy-revalidate ");
  4. // The cache expiration time is 24 hours (Expires)
  5. $ Offset = 3600*24;
  6. $ Expire = "Expires:". gmdate ("D, d m y h: I: s", time () + $ offset). "GMT ";
  7. Header ($ expire );
  8. // Last-Modified, Last Modified time
  9. $ Gmt_mtime = gmdate ('d, d m y h: I: S', time (). 'gmt ';
  10. Header ("Last-Modified:". $ gmt_mtime );
  11. // Enable gzip compression
  12. Ob_start ();
  13. ... Your php code...
  14. ... Your php code...
  15. ... Your php code...
  16. Header ('content-Length: '. ob_get_length ());
  17. 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:

  1. SetOutputFilter DEFLATE
  2. DeflateCompressionLevel 5
  3. AddOutputFilterByType DEFLATE text/html text/css image/gif image/jpeg image/png application/x-javascript
  4.  

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. open C: WindowsSystem32inetsrvMetaBase in the text editor. xml (backup is recommended), 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 the set attributes are the same.

To compress dynamic files, set HcDoDynamicCompression to "TRUE", and add the dynamic file suffix to HcScriptFileExtensions, such as aspx;

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

Add the extension names of the static files to be compressed in HcFileExtensions, such as xml and css. HcDynamicCompressionLevel and HcOnDemandCompLevel indicate the 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:We recommend that you set these two attribute values to 9, which is cost-effective.

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.

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