1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

EASY way to improve your php site!

Discussion in 'Site & Server Administration' started by nevetS, Aug 14, 2005.

  1. #1
    Here is a one line, super easy way to improve your site - and it can all be done in .htaccess.

    Here's the solution, read on to see what it does:

    php_flag zlib.output_compression on
    PHP:
    Compression is turned off on php by default. It may not work if you are doing something complex (like playing around with your output buffers), but for most php output you shouldn't have a problem.

    You can go to http://www.whatsmyip.org/mod_gzip_test/ (not my site) to see if your site currently has compression enabled. If it doesn't, try adding the above line to your .htaccess file and verify whether it works or not.

    Basically, what it does is it compresses (or "zips") your php page before sending it out to the visitors browser if the browser supports compression (most do, but there are a few exceptions).

    As an example, here's a page I just tried this on:
    [FONT=Arial, Geneva, Helvetica, SunSans-Regular, sans-serif] Original Size: 59 K
    Gzipped Size: 10 K
    Data Savings: 83.05%
    [/FONT]My visitors now see the page in about a sixth of the time they used to. If you are using apache 2.x you can compress your static html output with mod_deflate. With apache 1.3.x you can use mod_gzip. Both are relatively easy to implement, but not nearly as easy as adding a single line to your php file.

    Also - I'm pretty sure that the spiders support gzip compression - it would be silly for them not to, so think about your 100,000 page site getting crawled and taking 1/6th of the bandwidth!

    I'm not going to spell out all of the options here, but for those interested there are ways to adjust the level of gzip compression used by php to either make it easier on your cpu load or to save even a few more bytes of data.

    You can also set this as a server wide option in your php.ini file instead of .htaccess.

    Do it today! And give me a green rep. I'm getting way behind the rest of these guys :).
     
    nevetS, Aug 14, 2005 IP
    dct and Hodgedup like this.
  2. fryman

    fryman Kiss my rep

    Messages:
    9,604
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    370
    #2
    Are there any potential problems with this? Will it work on any site? Does it affect the seo work done on the pages? Seems too good to be true, although it would be great if did work, did you try it on several of your sites?
     
    fryman, Aug 14, 2005 IP
  3. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #3
    • running on a mac server: http://pear.php.net/bugs/bug.php?id=948
    • register_shutdown_function() will not output anything with zlib.compression enabled
    • using this header: header('Content-Length: '.ob_get_length()); will not work properly (it will output the content length of the uncompressed file)
    • Using the flush() function will cause output to be sent uncompressed, but the header will say that the file is compressed. IE seems to handle this ok, according to a post I saw.

    Like I said (or at least meant to say)... Most sites will not have any problems. If you are doing something complex, you probably want to test the output.
     
    nevetS, Aug 14, 2005 IP
  4. fryman

    fryman Kiss my rep

    Messages:
    9,604
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    370
    #4
    There's also an option to do it via php.ini so it will work on all your server... I'll have to ask my admin what he thinks about this. I hope it can be done, sounds great
     
    fryman, Aug 14, 2005 IP
  5. forkqueue

    forkqueue Guest

    Messages:
    401
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This will up the CPU usage on your server. For VPSs I'd recommend leaving it off - it's likely to make things slower.

    I've you've got pages with a lot of HTML on them (forums are a common example) and your server has plenty of CPU power then it can give you a (small) reduction in bandwidth usage, and improve percieved speed for the end user.
     
    forkqueue, Aug 15, 2005 IP
  6. muchacho

    muchacho Active Member

    Messages:
    310
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #6
    The bandwith reduction is great. If you don't have a problem with cpu load is a good idea to use it (or at least try it). The level of compression is important also. It ranges from 1 to 9. 1 means less compression and less cpu load.

    I use this on 1 server (on the apache conf file):

    php_flag zlib.output_compression On
    php_value zlib.output_compression_level 1
     
    muchacho, Aug 15, 2005 IP
  7. SERPalert

    SERPalert Guest

    Messages:
    1,003
    Likes Received:
    66
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Really good article here. Pros cons.

    http://www.webmasterworld.com/forum19/2828.htm

    I don't use it.

     
    SERPalert, Aug 15, 2005 IP
    yfs1 and nevetS like this.
  8. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #8
    digitalpoint, Aug 15, 2005 IP
    nevetS likes this.
  9. RoyalCheats

    RoyalCheats Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for the tips ill be looking into this for my site :)
     
    RoyalCheats, Aug 15, 2005 IP
  10. JoeO

    JoeO Peon

    Messages:
    431
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I tried this site and I guess this option is already on for my site:

    Original Size: 123 K
    Gzipped Size: 16 K
    Data Savings: 86.99%

    Not bad, but I have tons of bandwidth to spare anyways.
     
    JoeO, Aug 15, 2005 IP
  11. SERPalert

    SERPalert Guest

    Messages:
    1,003
    Likes Received:
    66
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Wow lots of green rep for my reply! Thanks hope it helps you guys out.
     
    SERPalert, Aug 16, 2005 IP
  12. blinxdk

    blinxdk Peon

    Messages:
    660
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I admin a large web server cluster for a financial newspaper and we have been using compression for a long time, it drasticly lowers our bandwith usage (everything is php). I can also recommend eaccelerator (formerly turck-mmcache). it has helped us reduce serverload and is a great alternative to zend which we had some trouble with.
     
    blinxdk, Aug 16, 2005 IP
  13. fine

    fine Peon

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Does it work for normal HTML files too? or only work for PHP files?
    Thanks for your information.
     
    fine, Aug 24, 2005 IP
  14. fryman

    fryman Kiss my rep

    Messages:
    9,604
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    370
    #14
    I tired it with my VPS and it made the load go up and slowed down my sites :(
     
    fryman, Aug 24, 2005 IP
  15. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #15
    For HTML files, you can use mod_gzip (apache 1.3x) or mod_deflate (apache 2.x).

    I don't personally have any VPS experience, but my load levels are very low on my servers. VPS usually have several installs of apache and php running in separate address space though so I can see why it might not be a good solution with all the resources being expended. I'm curious as to what the load level on your VPS typically is without doing this?
     
    nevetS, Aug 24, 2005 IP
  16. KIbeth

    KIbeth Active Member

    Messages:
    374
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    60
    #16
    Cool, original size 7kb, new size 1 kb. : p
     
    KIbeth, Aug 24, 2005 IP
  17. RectangleMan

    RectangleMan Notable Member

    Messages:
    2,825
    Likes Received:
    132
    Best Answers:
    0
    Trophy Points:
    210
    #17
    well VPS environment normally have low memory...that could be the slow-down problem

    Also I use gzip and eaccelerator as well and I LOVE IT! My server load went from 1.5-2 to .2-.75 and page loads are faster too.
     
    RectangleMan, Aug 24, 2005 IP
  18. fryman

    fryman Kiss my rep

    Messages:
    9,604
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    370
    #18
    What did you use it on? A dedicated?
     
    fryman, Aug 24, 2005 IP
  19. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #19
    I'm using it on a dedicated server, yes. It might as well be shared with all the hosting of sites I'm doing on it, but the majority of the sites are very light php or straight html.

    I personally have not seen an average load increase at all.
     
    nevetS, Aug 24, 2005 IP
  20. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #20
    I've tried getting zlib working with no luck whatsoever.

    I downloaded the php-4.4.0 source, extracted it, did a ./configure (using --with-zlib amongst other things), make, make install, and restarted apache. There were no errors, but when I try phpinfo() there's no mention of zlib and it says the build date is back in September.

    Obviously I'm being thick here. Any ideas?

    (SuSE 10, Apache 2.0)
     
    void, Jan 5, 2006 IP