A Simple Way to Save Bandwidth in PHP

Posted by Nessa | Posted in , | Posted on November 18, 2007

12

If you were ever looking for a quick and sexy way to save bandwidth on your PHP-based porn high traffic site, all you have to do is add these two lines to the beginning of your PHP scripts:

<?php
@ini_set('zlib.output_compression_level', 1);
@ob_start('ob_gzhandler');
?>

This will automatically tell the server to compress the PHP page before sending it to the browser. However, sometimes this can causes excess load and a decrease in the speed of your website, but it’s hardly noticable.

Be Sociable, Share!

Comments (12)

xD, oh how funny …porn
if the page is large.. that’s going to use some memories…

This is more useful if you cache as well as compress, so that the server only has to compress once per day or however long the cache is set to last.

High traffic sites should avoid this though. Gzip compression is hard on CPU cycles and increases load under lots of traffic. But you’re correct for lower traffic sites (sub 100k impressions/mo)

:)

This is a good alternative however and said by other people here is that compressing the data before it’s sent to the browser it will suck a lot of server resources on a high demand. There are another alternatives like writing your own code and make use of the filesystem speed. For example if you have a high traffic site you could generate the first page (cover) every N minutes and write the content as a plain html file, then when someone reaches your site they will see a cached page of your first page. I’m not sure if my explanation was good enough but feel free to ask anytime :) thanks for sharing!

Rather than that, you’d probably want to just use a caching module for apache or lighttpd (or whatever http daemon you’re running). Though, that will only help execution time, not bandwidth. Plaintext doesn’t take much space ;)

If bandwidth saving is the aim, it’s a lot more effective to look at images. First, minimize the number of images on each page to a bare minimum. Do as much as you possibly can with CSS and plain HTML. Then make sure your images are as small as possible without sacrificing quality, and there you’ll see your biggest improvements.

Blocking hotlinkers is a good one as well. I’ve seen a lot of our customers save up to 80% of their bandwidth and half of their memory/CPU usage just by keeping people from linking images from their site. I have a quickie here:

http://www.v-nessa.net/2007/03/19/keep-people-from-jacking-your-images/

As for server-side caching, mod_cache with Apache 2.2.x proves to be the best combination for Apache-based servers. If you have a choice though, lighttpd is the top runner

I’ve seen some pretty crazy stuff pulled off with NgineX as well, but I don’t know how compatible it is with everything.

As for caching plugins with lighttpd, mod_mem_cache is the best for throughput, though if you have a huge site, you’ll need a lot of RAM for that.

If anyone has experience with lighttpd on a cpanel server, one of the resource sites i co-run can use a quickie tutorial on how to install and configure it

Yeah I run it on my cPanel VE, though I baleeted cpanel cause it sucks.

I was about to write it up, when I realized I’ve already done so on a forum.

But it needed a little updating. So I did so, and posted it on my page:
http://www.dancewithgrenades.com/?page=37

It’s pretty useless to compress images and video since they’re already compressed, so the compression ratio is going to be terrible for the CPU cycles that you waste. The plain text content of your site is generally not big enough to even warrant compression. It was a great idea back in the days of 14.4 modems, but with a low B/W connection being 256-512Kb, it’s not so useful.

A better way to save space is to re-encode all of your images and video with a higher compression ratio, or use a file format that has a better compression ratio.

Although to sort of defeat my own argument, if you run a mailing list or other such site with alot of plain text content, it might be extremely useful. On the fly gzip compression will still skyrocket your load, but alot of http servers will allow you to have a gzip compressed version of a file and a plain one. This way if the browser sends

Accept-Encoding: gzip

It will send the gzipped version automagically. This will save you bandwidth at the cost of diskspace, not CPU cycles, which are more valuable on a high traffic website.

Well we all pretty much agree that using compression is not for high traffic sites (I stated this in the article as well). If you’re on a VPS or dedicated server or you have a low traffic site with just a lot of larger files then compression is more useful. We have a lot of customers that dont get a whole lot of traffic but use a shit ton on bandwidth. If anything, I would recommend caching for high traffic sites, and compression for lower ones.

[...] Fonte: v-nessa.net [...]

Post a comment