how to use gzip for a css/js file in PHP? - php

Now I'm not making use of any compression method,just put the below between the body tag:
<link rel="stylesheet" type="text/css" href="Client/Css/all.css" />
Is it possible to use ob_start("ob_gzhandler") in PHP to compress this css file?
And how?

this is not generally done programmatically, it is handled by the web server
For apache, there's a mod_gzip or mod_deflate module that you can use.
IIS has settings that you can use.

Yes you could (when piping the files through a php script that performs the compression). But there is much more to it than simply compressing the output. You should only send compressed output to clients that support it, you should make sure you properly handle caching information/requests. Etc.
In quite some cases your webserver contains support for doing all that for you without the need of a PHP script. In case of Apache look at mod_deflate and mod_mime (directive AddEncoding).

There are several ways.
You could tell the webserver to treat the file as PHP (renaming to have a .php extension would be the simplest) and then add:
<?php
header("Content-type: text/css; charset=utf-8");
ob_start("ob_gzhandler")
?>
to the top.
You could write a PHP script that does the same thing, but reads in the CSS file instead of having it inline.
Both this options lead to caching issues - you would have to take care of the cache control HTTP headers too if you want to be sane about it.
The best option would be to forget about PHP and just configure your webserver to compress those files (using mod_deflate, mod_gzip or similar)

If you are using a newer version of the apache web server add the following to your .htaccess file:
DeflateCompressionLevel 9
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|ico)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary

What are you trying to accomplish? If you're trying to shrink components to send to the clients browser so that they're downloaded more quickly, you should be using mod_defalte. If that's not an option, you can find more here: first result from googling "gzip php css"
EDIT: I just want to point out that unless you're using an older version of Apache, you should use mod_deflate not mod_gzip.

Related

.php files are not compressing, while GZIP is set in .htaccess

According to checkgzipcompression all my .js and .css are correctly GZIP compressed.
Except for my php webpages:
www.website.nl/webpages.php seem not gzipped, resulting in a 75% increase in "wasted" data transfer. My .htaccess file ends with the following below. What needs to be changed here to get GZIP compression for my php webpages to work properly? (Excluding the .php files from the /includes/ folder ofcourse since those are not "transferred to the browser" and are processed internally so they need no compression). Thanks!
# compress speficic filetypes
<IfModule mod_deflate.c>
<FilesMatch "\.(js|css|txt|php)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
Pages served in the browser with .php extension are considered html. PHP uses Content-Type "text/html" as default.
I would say, find out what's your web server, and then find out the correct compression method for it.
Good luck!

how to optimize loading time of my website? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am new to php developer and developed a website in magento 1.7.
My website take a too much time to loading.
I deleted caches , re indexing the date comprise images still take lot time for loading.
please suggest me how to optimize the loading time of my website.
thanks in advance .
Enable Output Compression
This section will turn on the apache mod_deflate module, which compresses text, css, and javascript before it is sent to the browser. This results in a smaller download size. To enable, simply uncomment the appropriate lines so that it looks like the following:
<IfModule mod_deflate.c>
############################################
## enable apache served files compression
## http://developer.yahoo.com/performance/rules.html#gzip
# Insert filter on all content
SetOutputFilter DEFLATE
# Insert filter on selected content types only
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# Don't compress images
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>
See more at: http://docs.nexcess.net/article/optimizing-magento-performance.html#sthash.ZUpf53Fy.dpuf
Enable Expires Headers
NOTE: This does not work on Litespeed servers.
Browsers use Expires headers to determine how long a page component can be cached. Static components, like images, should have far-future expires headers, but truthfully, all page components should have expires headers. To turn this feature on, just uncomment the appropriate line and add "ExpiresActive On" right above it. See below:
#
Add default Expires header
http://developer.yahoo.com/performance/rules.html#expires
ExpiresActive On
ExpiresDefault "access plus 1 year"
- See more at: http://docs.nexcess.net/article/optimizing-magento-performance.html#sthash.ZUpf53Fy.dpuf
Reference taken from here
This has worked for me to a good extent.
Try looking at these results: http://gtmetrix.com/reports/www.fashiza.com/KLDZeYuI
It seems like you'd gain alot in this case moving some of your javascript down and checking if all of your images have:
1) defined width and height in css.
2) format optimised for the web
EDIT: You are loading almost 500kb of javascript. This is probably the main reason. Try to compress your js and decide if you really need that much.
Tips for Magento site speed up
Summary:
Enable all Magento caching layers.
Configure your web server to enable Connection Keep-Alive, enable mod_expire, gzip compression.
Enable MySQL Query caching.
Install APC or XCache to cache PHP opcode.
Install Memcache and configure Magento to store session and cache to memory.
Install third-party Full Page Cache Magento extension.
Install Varnish cache server for full page caching on system layer.
You can check detailed instruction: http://www.magentocommerce.com/boards/viewthread/36225/
1.http://www.fashiza.com/media/js/1348d08285c79d6cda3eb91987e1942f.js
is too big 100KB
minimify that
2.http://www.fashiza.com/media/css/d2d005572b30b4d004b45f21ee567278.css
is too big
3.no file:productsliderpoint
google:compress js and you will find answer
i.stack.imgur.com/eh1NR.jpg
this is not normal
Create benchmarks to measure what causes the largest time lags. This helps you to narrow down the problem, thus, gain a better understanding, as your question is too broad to enable us to help you.

Looking for custom PHP Content Delivery Network (CDN) script for my server

REF: https://en.wikipedia.org/wiki/Content_delivery_network
A content delivery network or content distribution network is a system of computers where our website is stored so it’s data (images/videos) can be served from multiple locations.
However I dont want to use any online paid/unpaid CDN services but would like to setup CDN on my own high speed server. I did google a lot but dont see any such CDN script which i can install on my server.
I am looking for such script which can support High level cache-control.
Can you please share if you know any good CDN script developed in PHP?
This isn't done in PHP, this done in Apache.
What I've done on my own home server (that's probably what you want) is set up a cookieless sub-domain for serving content, and enable caching and GZip. The following Apache configurations are all located in a .htaccess file in the website directory.
# GZIP compression
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(eot|ico|gif|jpe?g|php|png|ttf|svg|woff)$ no-gzip dont-vary
# Fonts on a cookieless subdomain
<FilesMatch "\.(eot|ttf|svg|woff)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
# Cookieless Static Content
<FilesMatch "\.(css|eot|ico|gif|jpe?g|js|png|ttf|svg|woff)$">
Header unset Cookie
Header unset Set-Cookie
</FilesMatch>
# Caching
ExpiresActive On
ExpiresDefault A0
<FilesMatch "\.(eot|ico|gif|jpe?g|png|ttf|svg|woff)$">
# 2 year caching for images and stuff
ExpiresDefault A31536000
Header append Cache-Control "public"
</FilesMatch>
<FilesMatch "\.(css|js)$">
# 1 week caching for styles and scripts
ExpiresDefault A604800
Header append Cache-Control "public"
</FilesMatch>
#Other Header Manipulation
FileETag MTime Size
Header unset X-Powered-By
AddDefaultCharset UTF-8
DefaultLanguage en-US
So long as you don't mind caching and GZip on your primary domain (which you shouldn't), just link to your cookieless content using your designated cookielesss sub-domain, and Apache takes care of the rest.
Update
I added a few things I've learned about since posting this answer, such as:
Allowing any domain to link to fonts so that they may be served without cookies.
Setting the ETAG header since it should be set.
A few other header fields that aren't bad to include/get rid of.
However, there's one last security concern to keep in mind if you're using HTTPS, and that is BREACH. To protect against this decryption technique, you can remove gzip compression from any page that displays dynamic content (GZIPping static content like static HTML, CSS, or JS is still ok). To avoid compressing a certain file type (like PHP), add it to the SetEnvIfNoCase directive near the start of this config.
Alternatively, you can keep compression enabled and use the GCM cipher method since the BREACH family of attacks only work on the CBC cipher method. As much as I hate to be "that guy", the manual is really the best reference for this if you want to get into configuring such things. It's a fairly complicated topic and the manual does a good job of explaining the basics.

For php flush - how to disable gzip for specific file?

I have a ajax call to somefile.php . i want the php script to do a simple task and than send back data to the user, and only than do very time consuming tasks. so i need to flush the output after the first simple task. it doesn't work, probably because i have gzip enables.
I definitely don't want to disable gzip across all the vhost, and also not in all the folder where somefile.php is. i just want to disable it for this specific file. is that possible?
EDIT:
this is what i've included in my apache conf:
<FilesMatch \.php$>
SetEnv no-gzip 1
</FilesMatch>
this is my php script:
<?php
$sucesss = #apache_setenv('no-gzip', 1);
#ini_set('zlib.output_compression', 0);
#ini_set('implicit_flush', 1);
ob_start();
for($i=0;$i<10;$i++)
{
echo 'printing...';
ob_flush();
flush();
sleep(1);
}
?>
it doesn't work. i still see all the output together after 10 seconds.
I was looking for a solutions for the same issue. This is what worked for me but unfortunately it seams NOT to be a VALID header.
<?
header("Content-Encoding: none");
?>
apache_setenv() is correct. See the documentation.
http://php.net/manual/en/function.apache-setenv.php#60530
apache_setenv('no-gzip', '1');
Your problem is that you turned on output buffering with ob_start(). Comment that out.
I've learned that apache_setenv() is only available with the PHP Apache module. It's not available when using FPM. In that case, you have to use .htaccess to turn off GZip. An example is
https://stackoverflow.com/a/36212238/148844
RewriteRule ^dashboard/index - [E=no-gzip:1]
SetEnvIf REDIRECT_no-gzip 1 no-gzip
The - means NOOP, E means set variable, 1 is the value. After redirects, the variables are renamed and prepended with REDIRECT_.
If the output is still being buffered, check if you are going through a proxy or cache. See if headers like Via: 1.1 varnish or Via: 1.1 vegur are present. They will buffer the response also.
Put this in httpd.conf
# exclude certain page requests (e.g. for requesting getMyFile.php?action=getFile&id=3 as non-compressed)
SetEnvIfNoCase Request_URI getMyFile\.php$ no-gzip dont-vary

How do I gzip my web files

As prescribed by Yahoo!, gzip'ng files would make your websites load faster. The problem? I don't know how :p
http://www.webcodingtech.com/php/gzip-compression.php
Or if you have Apache, try http://www.askapache.com/htaccess/apache-speed-compression.html
Some hosting services have an option in the control panel. It's not always possible, though, so if you're having difficulty, post back with more details about your platform.
If you are running Java Tomcat then you set a few properties on your Connector ( in conf/server.xml ).
Specifically you set:
compressableMimeType ( what types to compress )
compression ( off | on | )
noCompressionUserAgents ( if you don't want certain agents to receive gzip, list them here )
Here's the tomcat documentation which discusses this:
http://tomcat.apache.org/tomcat-5.5-doc/config/http.html
Edit your httpd.conf file.
Add this line to load the module:
LoadModule deflate_module modules/mod_deflate.so
Add these lines to actually compress the output:
AddOutputFilterByType DEFLATE text/css text/html application/x-javascript application/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Jetty will look for gzip'd versions of static files, as well as it has a GzipFilter for dynamic content.
You could probably pull the GzipFilter over into Tomcat if you wanted more control over compression than just Tomcat's connector-level compression...
http://docs.codehaus.org/display/JETTY/GZIP+Compression
Gzip compresses your webpages and cascade style sheets before sending them over to the client browser.
This drastically reduces transfer time since the files are much smaller.
There are different methods of setting up gzip compression depending on whether or not you've got an IIS or Apache server
Example: this link.
http://developer.yahoo.com/performance/rules.html#gzip
This is the reference if any asks me about my reference loading gzipped files
If you are using Lighttpd, there is mod_compress.
Seeing how most answers here are almost 5 years old, here's some very current and up to date example references.
For example server configs that enable gzip/deflate type compression for iis, lighthttpd, nginx, and even node see: https://github.com/h5bp/server-configs
For a very good current implementation of Apache mod_deflate see
https://github.com/h5bp/html5-boilerplate/blob/master/.htaccess#L156
Gzip compresses your webpages and cascade style sheets before sending them over to the client browser
other example this link

Categories