Compressing my website using php

Hi everybody,

Any assistance with the following will be appreciated.

I have the following website: http://www.cursos-chino.es , when I run it trough http://www.seositecheckup.com/ it tells me I need to compress the site.

I asked my webhost and their reply was to add the following to all active files:

at the beggining of the file before anything else:
<?php ob_start(“ob_gzhandler”); ?>
at the end of the file:
<? php ob_flush(); ?>

Then save the file as .php

Now I have 2 problems:

  1. should I do this for all files, including .html, .css and .js as well as .php files I use for forms, or only for .html files?

  2. once I do this changes on my .html file I get a parsing error on line 2 <?xml version=“1.0” encoding=“UTF-8”?>

My .html files start with the following:

<?xml version=“1.0” encoding=“UTF-8”?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

and when I add the php line it looks like

<?php ob_start(“ob_gzhandler”); ?>
<?xml version=“1.0” encoding=“UTF-8”?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

and apparently the php considers the xml as php and keeps telling me its wrong, so I don’t really know how to continue.

I also looked at the solution given by http://rakaz.nl/2006/12/make-your-pages-load-faster-by-combining-and-compressing-javascript-and-css-files.html

but it looks quite complicated to do it right for somebody with no IT background.

Thx for your help.

If you’re using Apache, you could use mod_deflate and leave your code alone.

If you cannot configure the compression to be done via the web server.
DO NOT IMPLEMENT COMPRESSION. PERIOD.

Furthermore, you should remove “<?xml version=“1.0” encoding=“UTF-8”?>” from your html files.

Thx for the info everybody.

logic_earth : any reason why I should remove the “<?xml version=“1.0” encoding=“UTF-8”?>” ? I just copied it from website design books and I understand it sets the carachter encoding, right? it’s not necessary to include it?

HTML pages should use a HTML doctype, not the XML opening tag. You can set them to UTF-8 with a meta tag, and save the files as UTF-8 from your editor.

Thx all for your help.

Cheers.

The “<?xml version=“1.0” encoding=“UTF-8”?>” will trigger what is known as “Quirks Mode” in some (all?) browsers. You want “Standards Mode” and, to get that, you need a DOCTYPE.

Hello.

You can compress your site in php aplication with deflat or gz-ip (recomend). Your HTML data will reduced and speed up. If you compress meybe you need to check if user-agent support this method and which one.

What’s “gz-ip”, and how does your answer cover what Anthony and logic_earth didn’t mention already? What do you mean by

If you compress meybe you need to check if user-agent support this method and which one.

Are there any other methods?

Just curious… why not?

Doing it via PHP is incredibly slow, not to mention you would have to run every piece of CSS, HTML, JavaScript through PHP. On a shared server this setup can get you in trouble pretty quickly. In fact using output compression on a shared server in any form can get you in trouble. A lot more resources (CPU, RAM, etc) are required to implement compression. Over using those resources (on a shared server) will not go over well with the hosting company.

Either way, doing the compression though PHP, you pretty much lose the point of compressing. While it will download faster, the processing time required to compress offsets that faster download. Requiring the user to wait longer before the download even starts.

Hello.

Old browser doesn’t support get data compresed like IE 6 and if you set this in your application make one if which check if browser support or doesnt support compression. You can check with If “Accept-Encoding” has “gzip” or and “deflate”.

Hi sebasforums

I have just set up a new WordPress site for the company I work for and have set up site compression.

Depends on the server set up, but you could try this

  1. Create a new file called php.ini in you root directory
  2. Add the following code to the file:
    zlib.output_compression = On;
    zlib.output_compression_level = 9;
  3. Upload to your server.

As i say this has worked for me, hope it helps.

Do you have some benchmarks to show the difference between compresisng using PHP or compressing using the web server (e.g. mod_deflate)? Again, just out of curiosity, I always enjoy reading about performance related stuff :wink:

As for benefits and downsides, I guess it has to do with which is more expensive: bandwidth or processing power. I have no idea about that though.

My personal favourite way of handling this kind of stuff is to compress using either PHP or mod_deflate, but also sending headers for the correct caching of the resources in the browser (i.e. an image that never changes, browser-cache it for a month or even a year).