Continued from page 1
$file = $_SERVER["PATH_TRANSLATED"]; readfile($file);
"GIFT WRAPPING" YOUR OUTPUT
To make HTTP compression work, we use two functions: ob_start() and ob_gzhandler(). Output buffering functions are strange. Any time you try to display something, you can have PHP save up everything you're trying to output. At very end it's all dumped into a function of your choosing where text can be changed or transformed before it's output.
There is a built-in PHP function called ob_gzhandler() which takes one parameter (a string of text), compresses data according to gzip standard and does all header trickery that's needed to tell user's browser that we are transmitting data that needs to be decompressed once it's downloaded. When this line is used:
It tells PHP: everything displayed afterwards has to go through function ob_gzhandler() first. Put that at top of our script and here's what we've got:
ob_start("ob_gzhandler"); $file = $_SERVER["PATH_TRANSLATED"]; readfile($file);
Save that as compress.php. Upload both files, chmod htaccess.txt to 0755 and rename to .htaccess and you're done. That's all you need for it to work, and you can just as easily apply HTTP compression to any script by just adding that line.
To try this puppy out, I got on a dialup connection and put a copy of "The Decline And Fall Of The Roman Empire Volume 1" on my web host, a 900 page book, about 1.6 megabytes in size. Without HTTP compression it took 5 and a half minutes to download. With compression, only 2 minutes. Internet Explorer told me download was going at 20 KB per second, impossible with a dialup connection... but since file was zipped, I really was downloading 20 KB a second (once data was decompressed on my end) over a 5 KB per second connection.
Though HTTP compression will work on sounds, video, and images, space you save is negligible, usually only a few bytes. These sorts of media are already heavily compressed so zipping makes almost no difference. This is why we've told htaccess to only use compression on text and HTML, because it's with human languages like English where a lot of repetition occurs, which means more information can be compressed.
Not all browsers support HTTP compression, but ob_gzhandler() figures out if a browser can support HTTP compression. If browser doesn't, original file is displayed, no harm done.
You can get a copy of this sample script at: http://www.jumpx.com utorials/wrapper/compress.zip
Both of these scripts I've created for you will work only on static files, files that actually exist such as images or HTML files. If you tried to apply these wrappers as-is to PHP scripts, Perl scripts or even HTML pages that use SSI. If your whole site is run by a single script it's a better idea to hard-code these things right in, anyway.
THE BEST THING SINCE BUBBLE WRAP
This last demonstration of an htaccess wrapper is something that I think most people with content sites have a use for. On Internet, people steal stuff. Theft of HTML source code is a nuisance, sure, but lifting of images is more common. Someone likes a logo on your page, or an e-book cover, or a picture of a physical product you're selling, and it becomes theirs to use.
A practical way to keep this from happening is to add a watermark to all your images, which is your logo or name on a corner somewhere, forcing anyone who takes your graphic to either unwillingly give you credit, or chop off a part of that picture.
Lucky for us, PHP has a set of functions to handle images, and in version 4.3 and above, it's included by default. Wrappers come in handy here because you might have an entire site full of images and would rather not spend three weeks watermarking tons of images by hand. Maybe you just don't want to have to juggle two sets of images, one watermarked and one normal.
Download this script from: http://www.jumpx.com utorials/wrapper/watermark.zip
The only files you need to worry about in that zip are htaccess.txt and wrapper.php. Upload them to a folder called "watermark", chmod htaccess.txt to 0755 and rename to ".htaccess".
The file wrapper.php remain as is. I've put comments in file regarding most of what it does, so if you're curious go ahead and take a peek.
What script does is this: It figures out original image that was supposed to be called. Then it loads watermark, which I've set in wrapper.php to be "watermark.png" which is just a PNG image containing text "THIS IS WATER MARKED". The watermark is placed on top of original, in lower right corner, and output in same format (i.e., JPEG) as original.
You can tell difference by looking at these two images: http://www.jumpx.com utorials/wrapper homas.jpg http://www.jumpx.com utorials/wrapper homas-watermarked.jpg
I've included several types of images (GIFs, JPGs, and PNGs) in zip file for you to test out. Once you've got everything setup, upload those images and see how they look with watermark.
This script will work with GIFs, JPEGs, and PNGs. Due to a patent issue (which expires worldwide in July 2004) GIFs can only be read, and not output. To make up for this, any of your GIFs will be output as PNGs, which should still work.
If you think about it, a watermark script like this could also be used for a number of things. For example, if you decide to run an image hosting service like AuctionWatch does for eBay users, you could watermark your site's URL to bottom. Your users get a free service and everyone else sees a possible place to get free image hosting, there's some nice viral promotion right there.
You could also adapt script to check HTTP referer (in variable $_SERVER["HTTP_REFERER"]) to see if image was called offsite. If it was, script would put watermark on there but if you called it from a page on your own site, image would be shown without one.
Robert Plank is the creator of Lightning Track, Redirect Pro, Rotatorblaze, and other useful tools.
Want to pick up more programming skills? Then purchase the e-book "Simple PHP" at http://www.simplephp.com
You may reprint this article in full in your newsletter or web site.