Check and Use GZIP Compression through PHP

Back to Blog
gzip compress hire web designers in mira road mumbai - ezeelive technologies

Check and Use GZIP Compression through PHP

gzip compression is using for increase website speed and save server bandwidth.

1. To enable on your website, you typically configure it on the server side. Here’s how to enable for different web servers:

Configure Apache Web Server for GZIP Compression

Apache’s mod_deflate module provides the Gzip compression functionality. Ensure that it’s enabled in your Apache configuration.

LoadModule deflate_module modules/mod_deflate.so

Add the following configuration directives to your Apache configuration file (e.g., .htaccess or httpd.conf) to enable compression for specific MIME types:

    # Compress HTML, CSS, JavaScript, Text, XML and fonts
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
    AddOutputFilterByType DEFLATE application/x-font
    AddOutputFilterByType DEFLATE application/x-font-opentype
    AddOutputFilterByType DEFLATE application/x-font-otf
    AddOutputFilterByType DEFLATE application/x-font-truetype
    AddOutputFilterByType DEFLATE application/x-font-ttf
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE font/opentype
    AddOutputFilterByType DEFLATE font/otf
    AddOutputFilterByType DEFLATE font/ttf
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE image/x-icon
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/xml

    # Remove browser bugs (only needed for really old browsers)
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    Header append Vary User-Agent

Configure Nginx Web Server for GZIP Compression

Add the following configuration to your Nginx server block:

gzip on;
gzip_disable "msie6";

gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml;

2. Compress all the CSS and js file using 7-zip e.g. if your CSS file name is style.css it will save as style.css.gz.
3. Upload all the file on live server and set Content Encode GZIP to .gz files
4. Define gzip global variable in your common PHP file (remember the common file will include in all the PHP file into your website)

 $gzip_string="";
 if (substr_count($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip"))
 {
 $gzip_string=".gz";
 }else{
 $gzip_string="";
 }

5. Put defined variable in all your script and style including in your PHP pages e.g.

 <link rel="stylesheet" type="text/css" href="css/my-style-file.css<?php echo $gzip_string;?>" />
 <script type="text/javascript" src="js/my-script-file.js<?php echo $gzip_string;?>"></script>

After configuring, test your website’s performance using tools like Google PageSpeed Insights or GTmetrix. They can show if compression is enabled and its impact on your website’s loading time.

Share this post

Comment (1)

  • Veem Reply

    Generally Receive a Next Viewpoint

    March 11, 2014 at 1:35 am

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Blog