How to integrate gzip compression in php site? - php

Want to integrate gzip compression on my php site , but while I am implementing the code ob_start("ob_gzhandler"); it's giving me an error 'This webpage is not available' although the page exist in the server , gzip compression is also enabled in the server. Please suggest a way to implement it.

Try this, Hope it'll help you
at the top of your index page put it
ob_start("ob_gzhandler");
And you need to do little more. Just write down below in your .htaccess
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</IfModule>

Related

Can't resolve Google pagespeed "Enable compression" point using Yii2

Site in question is http://burghleys.com/
Having trouble trying to improve the Google pagespeed score and says I need to enable compression with a 70% improvement on the JS file for example but I can't make the same improvement at all!
https://developers.google.com/speed/pagespeed/insights/?url=http%3A%2F%2Fburghleys.com%2F
I have GZIP enabled and use the apache mod_deflate to do so, tested as per http://checkgzipcompression.com/?url=http%3A%2F%2Fburghleys.com%2F
I use yii2 https://github.com/rmrevin/yii2-minify-view plugin to compress my files which generally works really well.
I use the HTML5 boiler plate htaccess file.
I tried manually compressing the JS file but can only reach 25%. CSS is even less yet states i can save 80%.
Any thoughts?
ps. I'm working on the other issues too, wanna get that 99 score.
You do have GZIP enabled for some items but if you follow the link to your js you will see that this resource is not being zipped up.
http://checkgzipcompression.com/?url=http%3A%2F%2Fburghleys.com%2Fminify%2Fb329b2d2ce53b5c6be05decfe5e5d723527a666e.js
Add the following line to your .htaccess
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/xml application/xhtml+xml application/rss+xml application/javascript appliction/x-javascript

Setting gzip from .htaccess not working [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I have a wordpress blog stuck on a cheap shared host at the moment.
I am trying to setup gzip compression from the .htaccess file, I have tried this...
# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
But that does not seem to work.
Now if I set it from my a PHP file like this...
if(extension_loaded("zlib") && (ini_get("output_handler") != "ob_gzhandler"))
add_action('wp', create_function('', '#ob_end_clean();#ini_set("zlib.output_compression", 1);'));
The PHP method successfully gzips my php pages, but I would prefer to do this from the .htaccess file so that I can automatically compress javascript and css files as well without running them through PHP.
UPDATE
After much testing, I believe possibly it does not work from my .htaccess because maybe the shared server does not have this enabled, so the only way to compress anything would be through PHP, bummer

Prevent mod_deflate on zip file served by PHP

I'm having some trouble prevent mod_deflate from jumping in on this scenario:
user running CodeIgniter (or any other framework that re-directs to index.php)
mod_deflate is active
zip file is served by a CodeIgniter controller (headers + readfile)
The thing is that Apache always detects the content as being php and therefor something like the lines bellow wont work as the server assumes the ZIP file as being a PHP one.
<FilesMatch "\.(xml|txt|html|php)$">
SetOutputFilter DEFLATE
</FilesMatch>
Any ideas on how I can have Apache distinguish from an HTML file or a ZIP file both generated by the same index.php framework file.
Edit:
apache log
[Mon Jun 20 02:14:19 2011] [debug]
mod_deflate.c(602): [client 192.168.0.5]
Zlib: Compressed 50870209 to 50878224 : URL /index.php,
referer: http://demo.dev/
Edit:
CI controller that serves the zip
header('Content-Type: application/zip');
header('Content-Transfer-Encoding: binary');
header("Content-Length: " . filesize($file_location));
header('Content-Disposition: attachment; filename="' . $file_title . '"');
readfile($file_location);
Even tough all answers should have been perfectly valid in a reasonable scenario (and were actually tested prior to making the question) the reason to why I've been unable to instruct Apache to deflate a file by MIME-Type remains unknown.
I was able to have it work as desired by forcing the following instructions into the script
apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);
I do understand that this is a hot patch and is not addressing the problem's root but so far that will have to suffice. As there are others who may hit the same flag, the above code stays here for reference in what is a dirty fix.
You can either:
use the deprecated AddOutputFilterByType and specify only the content types you do want to filter; or
use the more powerful mod_filter. In FilterProvider you can provide a rule that excludes the filter when the zip content type (application/zip) is found in the response headers.
You can make use of mod_rewrite to change the mime-type of the request on the Apache level:
# Serve .zip request as zip-files
RewriteRule \.zip$ - [T=application/zip,E=no-gzip:1]
Place it above the rules of the framework, however this needs to make DEFLATE as well depended on mime-type and not file-extension as you do with <FilesMatch>.
Probably it works well together with
AddOutputFilterByType DEFLATE text/html
instead of the <FilesMatch> Directive.
Edit: Added the L flag which should be used in .htaccess context and additionally turned DEFLATE off via the no-gzip environment variable.
Try this (since your urls appear to end in .zip it might work for you):
<FilesMatch "\.(xml|txt|html|php)$">
SetEnvIf Request_URI "\.zip$" no-gzip
SetOutputFilter DEFLATE
</FilesMatch>
Instead of using
<FilesMatch "\.(xml|txt|html|php)$">
SetOutputFilter DEFLATE
</FilesMatch>
Use this configuration for setting compression rules.
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/x-javascript application/javascript
This way, your output will be compressed only if content-type matches with above directives.
CI controller that serves the zip is already sending correct content-type header, so this will not get compressed.
header('Content-Type: application/zip');

Compressing a webpage on Apache without gzip or deflate enabled

I have a joomla site on a shared server consequently I dont have access to apache to enable gzip or defalte so im wondering what i can do maybee with php to enable compression to speed up my site?
You can try to turn on output compression by setting the zlib.ouput_compression setting:
http://php.net/manual/en/zlib.configuration.php#ini.zlib.output-compression
or you can specifically do output buffering and run it through the GZIP handler:
ob_start("ob_gzhandler");
the first option is the preferred method, if you do use the second method check that it doesn't break any ouput buffering Joomla does - I'm no Joomla expert but I would expect a CMS to provide an option to compress content before delivery.
If gzip enabled on your host you need to add this lines to .htaccess file without change your code
AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript
AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
this lines enables apache deflate in html, css json and xml content
It would be nice if you add the condition to check if it accepts encoding like this.
<?php
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
ob_start("ob_gzhandler");
else
ob_start();
?>
Note:
you will have to do this for every php file, if your server doesn't support .htaccess configuration.

Compress php script before upload to main server

How can we compress php script before upload to main server so that our web site will become so fast to browse & reduce page downloading time..?
Thanks.
PHP runs entirely on the server-side, so compressing it will not reduce page download time.
You can't. You probably meant to compress the output of your website. You can add a mod GZIP handler to your Apache configuration, provided you run your site on Apache. Add the following to your .htaccess file:
# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
# Or, compress certain file types by extension:
<Files *.html>
SetOutputFilter DEFLATE
</Files>
Compressing a PHP file will not speed up it's execution time.
By default, PHP will read the entire file each time, compile it to bytecode, then run that bytecode.
What you are probably looking for is a bytecode cache like APC. It will cache the bytecode that is generated so it does not need to be created each time.
We've used NuSphere PhpExpress - Free PHP accelerator (http://www.nusphere.com/products/index.htm). It compiles PHP. It also requires an extension add-on for the server. It does speed pages up (a LITTLE), and it also protects them from being 'shared' (as the code is 'hidden').
There are better ways to increase the speed of you website though (see post by TheGrandWazoo) or by making sure your database table are optimized, queries and pages cached when feasible.

Categories