FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Site load in half time (with no browser compatibility issue)





imagefree
Here is the script. Add it to the top of your php script.
Code:
<?php
$ob_comp=!(strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip')===false)?true:false;
if($ob_comp)ob_start('ob_gzhandler');
?>

and then add it to your page's end
Code:
<?php
if($ob_comp)ob_flush();
?>



This script will gzip the output of the page where you have added it in the top.
The best thing with this script is that it will not do any compression if the browser doesnt support gzip. So, your website is always compatible with all browsers.
Peterssidan
How much will this affect the server load?
imagefree
if you are caching, the load will be minimum, however the benifit is the most.

Take my example. My page size was about 11 K and inline contents (images, css, js) were 100+K. After compression, my page contents were 3K and inline contents were 36K.
This is just one example and this is not the best of gzip. It can product mush better.


You may use server side caching and client side caching to reduce server load.

Use Appropriate headers for Client Side Caching ( i will specify later),
I have no experience of server side caching. Some other person here may help about this.
Agent ME
Is there any way to set Apache to do this by default for every page, image, file, etc?
kv
Yes you can. Just enable mod_deflate.

Also, you can enable it for either whole site, only for certain virtual servers, only for certain mime types or certain extensions.

Refer this url for more info.
inphurno
I have set this up recently in the pages of my site but when i check with YSlow i see that my php page has been compressed but not the css that was included like this:

<link rel="stylesheet" type="text/css" href="style/style.css" />

I have tried to setup the same ob_start('ob_gzhandler'); in my css file and rename it to style.css.php but this just seems to compress the style.css.php and not the index.php. this maybe cause i called ob_start('ob_gzhandler'); at the top of index.php and then later in style.css.php.

So how can i get both my css and my index.php compressed? and if both are compressed by just putting ob_start('ob_gzhandler'); at the top of index.php why does YSlow not see both ass compressed?
inphurno
kv wrote:
Yes you can. Just enable mod_deflate.

Also, you can enable it for either whole site, only for certain virtual servers, only for certain mime types or certain extensions.

Refer this url for more info.


i thought i read that at frihost mod_deflate has been disabled, this is what i see with phpinfo:

Loaded Modules: mod_security, mod_suphp, mod_php5, mod_python, mod_ssl, mod_frontpage, mod_setenvif, mod_so, mod_headers, mod_expires, mod_proxy, mod_auth_anon, mod_auth, mod_access, mod_rewrite, mod_alias, mod_userdir, mod_actions, mod_imap, mod_asis, mod_cgi, mod_dir, mod_autoindex, mod_include, mod_info, mod_status, mod_negotiation, mod_mime, mod_log_referer, mod_log_agent, mod_log_config, mod_env, mod_vhost_alias, http_core
kv
inphurno wrote:
I have set this up recently in the pages of my site but when i check with YSlow i see that my php page has been compressed but not the css that was included like this:

<link rel="stylesheet" type="text/css" href="style/style.css" />

I have tried to setup the same ob_start('ob_gzhandler'); in my css file and rename it to style.css.php but this just seems to compress the style.css.php and not the index.php. this maybe cause i called ob_start('ob_gzhandler'); at the top of index.php and then later in style.css.php.

So how can i get both my css and my index.php compressed? and if both are compressed by just putting ob_start('ob_gzhandler'); at the top of index.php why does YSlow not see both ass compressed?


when you have a <link> in your html, browser makes a separate http request to get the css. So if you want to compress css, you have to add ob_gzhandler to css file separately -- I mean, add it to both the files, not just one of them. <link> is not the same as include.
imagefree
Agent ME wrote:
Is there any way to set Apache to do this by default for every page, image, file, etc?


yes orcourse. you can add this line to your .htaccess


Code:
php_value auto_prepend_file ./begin.php
php_value auto_append_file ./end.php


and then add this code in the begin.php file in the same directory

Code:
<?php
$ob_comp=!(strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip')===false)?true:false;
if($ob_comp)ob_start('ob_gzhandler');
?>


and this one in the end.php file

Code:
<?php
if($ob_comp)ob_flush();
?>

this will force apache to do compression of every such file that is handled by php.
One important thing is that images should not be compressed using this technique. Images are already compressed and compressing them with gzip will simply add burden to the server and there will not be any further compression (or the compression will be insignificant, may be few hundred bytes).

In my under development site, all of the layout images, css and js (infact everything) is handeled by php so that i have a separate file files.php that output the appropriate requested file. For the rest of the site, i use just one file home.php, so all of my site depends on just 2 major files and both are controlled by php. So, it is most appropriate for me to either add the suggested code in the header and footer of each file or set .htaccess accordingly. Resultingly all of my js and css are compressed.
imagefree
inphurno wrote:
I have set this up recently in the pages of my site but when i check with YSlow i see that my php page has been compressed but not the css that was included like this:

<link rel="stylesheet" type="text/css" href="style/style.css" />

I have tried to setup the same ob_start('ob_gzhandler'); in my css file and rename it to style.css.php but this just seems to compress the style.css.php and not the index.php. this maybe cause i called ob_start('ob_gzhandler'); at the top of index.php and then later in style.css.php.

So how can i get both my css and my index.php compressed? and if both are compressed by just putting ob_start('ob_gzhandler'); at the top of index.php why does YSlow not see both ass compressed?


may be there is some caching problem. See in your FireBug (NET tab) whether the css file is requested with every page load ? and if yes, what are the response headers, and if there are no response headers for caching or expire, there may be some other problem/.
inphurno
ok i cant check this till later but should i have the ob_start("ob_gzhandler"); in both the style.css.php and the index.php?

also is this correct: <link rel="stylesheet" type="text/css" href="style.css.php" /> or should i add the stylesheet in another way?
imagefree
inphurno wrote:
ok i cant check this till later but should i have the ob_start("ob_gzhandler"); in both the style.css.php and the index.php?

also is this correct: <link rel="stylesheet" type="text/css" href="style.css.php" /> or should i add the stylesheet in another way?


Yes you have to include the both pieces of code in the begining and end of each file, and see the difference again in FF.

this is correct (and preferred) way to include stylesheet in HTML file. anyther way is @import that is less prefered.
inphurno
this is working now... thanks imagefree!! Very Happy

i didnt put the:

<?php
if($ob_comp)ob_flush();
?>

because i have it at the end of my index.php, is this ok?
imagefree
inphurno wrote:
this is working now... thanks imagefree!! Very Happy

i didnt put the:

<?php
if($ob_comp)ob_flush();
?>

because i have it at the end of my index.php, is this ok?


yes you need it just once in the end.
Stubru Freak
As far as I know this will disable progressive page loading. So this might not be a good solution for everyone.
inphurno
2 questions:

1) can the same technique that i used for css files be used for javascript files?
2) whats progressive page loading?
Stubru Freak
inphurno wrote:
2 questions:

1) can the same technique that i used for css files be used for javascript files?
2) whats progressive page loading?


It means that on slow Internet connections, the first part of the page is already shown while the last part is still loading. But with this technique, the page can only be shown when fully loaded.
inphurno
i'm still wondering can the same technique that i used for css files be used for javascript files?
Stubru Freak
inphurno wrote:
i'm still wondering can the same technique that i used for css files be used for javascript files?


Yes, why not. You can compress any content you want.
BlueVD
The best way to do the gzip inflation - deflation is by using apache.
Since is has a bit more juice to run on (php has some limitations).
Also, enabling the "mod_deflate" module would mean that all files served by apache get compressed (html, css, js, php, swf). This is very good for large sites that make extended use of js toolkits and shockwave flash content (swf's).
However, there are a few caveats: swf's that load content from the server can't use gzip and sometimes they do not specify whether they support gzip or not in the header (from what I recall, Adobe's site mentions something about older versions of the Flash IDE).
There's a lot of experimentation to do before enabling "mod_deflate" on the whole server, but in general it's worth it since CPU power is less cheaper that UPLOAD bandwith. =]
Fire Boar
Is there anything stopping you using:

Code:
php_value zlib.output_compression 16386


In the .htaccess file? Seems a lot easier and it has the boon that it doesn't need to be interpreted for every file.
imagefree
Fire Boar wrote:
Is there anything stopping you using:

Code:
php_value zlib.output_compression 16386


In the .htaccess file? Seems a lot easier and it has the boon that it doesn't need to be interpreted for every file.


yes there is a problem.

if you are using this method, you should disable Content-Length header or otherwise it may ruin the page in some browsers or may be in all browsers occasionaly.
inphurno
BlueVD wrote:
The best way to do the gzip inflation - deflation is by using apache.
Since is has a bit more juice to run on (php has some limitations).
Also, enabling the "mod_deflate" module would mean that all files served by apache get compressed (html, css, js, php, swf). This is very good for large sites that make extended use of js toolkits and shockwave flash content (swf's).
However, there are a few caveats: swf's that load content from the server can't use gzip and sometimes they do not specify whether they support gzip or not in the header (from what I recall, Adobe's site mentions something about older versions of the Flash IDE).
There's a lot of experimentation to do before enabling "mod_deflate" on the whole server, but in general it's worth it since CPU power is less cheaper that UPLOAD bandwith. =]


the mods at frihost have said that they will not enable gzip at the apache level Sad
Related topics
Hardware or Driver problems
How To : Improve Your PHP Programming
Designing a website tips
Opera Fans Site
Shouldn´t I use frames?
My Personal Web Site - especially on the Japan trip
photoshop websites
Browser Compatibility issues
Browser Compatibility issues
Old browser compatibility.
Have your layout coded for $50 frih!!! (XHTML & CSS)
Malwarebytes detect frihost as potentialy malicious website
Browser compatibility
JS: How do you manage Cross Browser Compatibility?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.