| HTACCESS is a remarkable tool you can use for | | | | file (in other words, just read its contents into a variable |
| password protection, error handling (like custom 404 | | | | and display it), our handler script would look like this: |
| pages), or HTTP redirects. It can also be used to | | | | |
| transform whole folders in seconds: adding headers to | | | | "GIFT WRAPPING" YOUR OUTPUT |
| all your HTML documents, watermarking all your | | | | To make the HTTP compression work, we use two |
| images, and more. | | | | functions: ob_start() and ob_gzhandler(). Output |
| A wrapper is like a middleman. Using htaccess you | | | | buffering functions are strange. Any time you try to |
| can tell your web server to "forward" certain files to | | | | display something, you can have PHP save up |
| PHP scripts of yours. When a visitor tries to load an | | | | everything you're trying to output. At the very end it's |
| image in their browser, you could activate a script that | | | | all dumped into a function of your choosing where the |
| adds a watermark to the image. When an HTML page | | | | text can be changed or transformed before it's output. |
| is loaded you could query an IP-to-country database | | | | There is a built-in PHP function called ob_gzhandler() |
| and have your HTML pages translated into the native | | | | which takes one parameter (a string of text), |
| language of your visitor's country-of-origin. | | | | compresses the data according to the gzip standard |
| Every file in a folder, or all files of a certain type in a | | | | and does all the header trickery that's needed to tell |
| folder, can be instructed to go through a PHP script. | | | | the user's browser that we are transmitting data that |
| TORTILLA WRAP | | | | needs to be decompressed once it's downloaded. |
| Pretend you host several affiliate sites, or a full-blown | | | | When this line is used:ob_start("ob_gzhandler"); |
| hosting service like Geocities. Most sites running on | | | | It tells PHP: everything displayed afterwards has to go |
| free hosting services have some kind of | | | | through the function ob_gzhandler() first. Put that at |
| advertisement the owners use to generate revenue. | | | | the top of our script and here's what we've got: |
| These aren't applied voluntarily by the users of these | | | | |
| services. The ads don't even show up on their source | | | | Save that as compress.php. Upload both files, chmod |
| files, just when displayed on the web. | | | | htaccess.txt to 0755 and rename to .htaccess and |
| It's possible to replicate this feature using less than 10 | | | | you're done. That's all you need for it to work, and you |
| lines of PHP and htaccess code. To start off, make a | | | | can just as easily apply HTTP compression to any |
| folder on your web host called "header". Create a new | | | | script by just adding that line. |
| text file and enter the following: | | | | To try this puppy out, I got on a dialup connection and |
| AddHandler headered .htm | | | | put a copy of "The Decline And Fall Of The Roman |
| AddHandler headered .html | | | | Empire Volume 1" on my web host, a 900 page book, |
| Action headered /header/header.php | | | | about 1.6 megabytes in size. Without HTTP |
| This designates files with the extension ".htm" and | | | | compression it took 5 and a half minutes to download. |
| ".html" to a type called "headered". The name | | | | With the compression, only 2 minutes. Internet Explorer |
| "headered" can really be anything, it's just a way of | | | | told me the download was going at 20 KB per second, |
| labeling a group of files. The last line there tells the web | | | | impossible with a dialup connection... but since the file |
| server that if any of the file types in the group called | | | | was zipped, I really was downloading 20 KB a second |
| "headered" are called, we should instead execute the | | | | (once the data was decompressed on my end) over |
| script "/header/header.php". This is the relative path, so | | | | a 5 KB per second connection. |
| if your URL is this will run | | | | Though HTTP compression will work on sounds, video, |
| That's all you've got to do for the htaccess file. Save | | | | and images, the space you save is negligible, usually |
| that as "htaccess.txt" -- we'll get back to it later. | | | | only a few bytes. These sorts of media are already |
| For the actual wrapper, create a new text file with the | | | | heavily compressed so zipping makes almost no |
| standard tags, then assign your header and footer file | | | | difference. This is why we've told htaccess to only |
| names to variables called $header and $footer. | | | | use compression on text and HTML, because it's with |
| $header = "header.html"; | | | | human languages like English where a lot of repetition |
| $footer = "footer.html"; | | | | occurs, which means more information can be |
| Redirecting a user to our script doesn't pass its | | | | compressed. |
| contents to it, just the filename. If you call phpinfo() in | | | | Not all browsers support HTTP compression, but |
| your script and scroll to the bottom you can see all the | | | | ob_gzhandler() figures out if a browser can support |
| server variables which give us the name. The element | | | | HTTP compression. If the browser doesn't, the original |
| "REQUEST_URI" in $_SERVER gives us the relative | | | | file is displayed, no harm done. |
| path (/header/sample.html), but we want the full | | | | You can get a copy of this sample script at: utorials |
| system path since we're going to be reading the actual | | | | wrapper/compress.zip |
| file (/home/username/wwwroot/your.host/header | | | | Both of these scripts I've created for you will work |
| sample.html), which is "PATH_TRANSLATED". | | | | only on static files, files that actually exist such as |
| $file = $_SERVER["PATH_TRANSLATED"]; | | | | images or HTML files. If you tried to apply these |
| The name of the file that just tried to be shown is now | | | | wrappers as-is to PHP scripts, Perl scripts or even |
| stored in the variable $file. Three simple things are left: | | | | HTML pages that use SSI. If your whole site is run by |
| output the header, output the actual file, then output the | | | | a single script it's a better idea to hard-code these |
| footer.readfile($header);readfile($file);readfile($footer); | | | | things right in, anyway. |
| That's it. Here's the entire header.php file: | | | | THE BEST THING SINCE BUBBLE WRAP |
| | | | This last demonstration of an htaccess wrapper is |
| All that, in just nine lines of code. Download it here: | | | | something that I think most people with content sites |
| utorials/wrapper/header.zip | | | | have a use for. On the Internet, people steal stuff. |
| That contains the htaccess file and PHP wrapper | | | | Theft of HTML source code is a nuisance, sure, but |
| script, along with a sample header, footer, and a test | | | | the lifting of images is more common. Someone likes a |
| page. Upload all five files to your web host, chmod | | | | logo on your page, or an e-book cover, or a picture of |
| htaccess.txt to 0755 then rename it to ".htaccess". It | | | | a physical product you're selling, and it becomes theirs |
| might disappear from your directory listing which is | | | | to use. |
| okay, it should still be there. | | | | A practical way to keep this from happening is to add |
| Load, in your browser, the copy of sample.html residing | | | | a watermark to all your images, which is your logo or |
| on your web server. The text "This is my header" | | | | name on a corner somewhere, forcing anyone who |
| should appear at the top while "This is my footer" | | | | takes your graphic to either unwillingly give you credit, |
| should show on the bottom. If you open up the actual | | | | or chop off a part of that picture. |
| file called sample.html, you'll see that these actually | | | | Lucky for us, PHP has a set of functions to handle |
| aren't there. They've been added in by the script all | | | | images, and in version 4.3 and above, it's included by |
| HTML files in the folder "header" must now pass | | | | default. Wrappers come in handy here because you |
| through. | | | | might have an entire site full of images and would |
| This is how wrappers work. Certain things, like adding | | | | rather not spend three weeks watermarking tons of |
| custom headers and footers are done "on the fly" | | | | images by hand. Maybe you just don't want to have to |
| without modifying your original file. You'll get the same | | | | juggle two sets of images, one watermarked and one |
| effect if you create other HTML files and upload them | | | | normal. |
| to this folder. | | | | Download this script from: utorials/wrapper |
| Files without ".html" or ".htm" extensions, such as text | | | | watermark.zip |
| files or images, won't show these headers or footers. | | | | The only files you need to worry about in that zip are |
| This is a good thing because text files aren't part of | | | | htaccess.txt and wrapper.php. Upload them to a folder |
| the presentation on a web site and adding extra text | | | | called "watermark", chmod htaccess.txt to 0755 and |
| to images will corrupt them. It affects all HTML files | | | | rename to ".htaccess". |
| within your /headers folder, and none of the files | | | | The file wrapper.php remain as is. I've put comments in |
| outside of it. | | | | the file regarding most of what it does, so if you're |
| If you wanted, you could add or remove any file | | | | curious go ahead and take a peek. |
| extensions you want, just by adding or taking away | | | | What the script does is this: It figures out the original |
| those "AddHandler" lines. | | | | image that was supposed to be called. Then it loads |
| To get everything back to normal, either delete your | | | | the watermark, which I've set in wrapper.php to be |
| .htaccess file or upload a blank .htaccess file in that | | | | "watermark.png" which is just a PNG image containing |
| folder, and all will be well again. | | | | the text "THIS IS WATER MARKED". The watermark |
| SHRINK-WRAP | | | | is placed on top of the original, in the lower right corner, |
| The same basic formula can be applied again for | | | | and output in the same format (i.e., JPEG) as the |
| other uses -- HTTP compression, for example. This | | | | original. |
| was an idea that used to be impractical because | | | | You can tell the difference by looking at these two |
| computers ran at slower speeds, and is now obsolete | | | | images: utorials/wrapper homas.jpg utorials/wrapper |
| because of broadband technologies (DSL and cable). | | | | homas-watermarked.jpg |
| It works like this: when an HTML page is loaded, the | | | | I've included several types of images (GIFs, JPGs, and |
| web server instead gives the visitor a zipped or | | | | PNGs) in the zip file for you to test out. Once you've |
| compressed version of that page. The visitor | | | | got everything setup, upload those images and see |
| downloads that file, which of course takes up less | | | | how they look with the watermark. |
| space than the real thing and downloads in less time, | | | | This script will work with GIFs, JPEGs, and PNGs. Due |
| then unzips it and displays the original page. | | | | to a patent issue (which expires worldwide in July |
| In this age of lighting fast DSL lines, there's almost no | | | | 2004) GIFs can only be read, and not output. To make |
| noticeable difference. However, if you have a site that | | | | up for this, any of your GIFs will be output as PNGs, |
| hosts large files whose audience is mostly dialup users, | | | | which should still work. |
| it might be something to look into. | | | | THE WRAP-UP |
| Make a new folder called "compress". Create your | | | | If you think about it, a watermark script like this could |
| htaccess file again, just as before, but set the | | | | also be used for a number of things. For example, if |
| extensions to include .htm, .html, and .txt. (The group | | | | you decide to run an image hosting service like |
| name, folder name, and script name have nothing to | | | | AuctionWatch does for eBay users, you could |
| do with one another, you can name any of these | | | | watermark your site's URL to the bottom. Your users |
| whatever you like -- I just like things to match.) | | | | get a free service and everyone else sees a possible |
| Our wrapper script for this should be called | | | | place to get free image hosting, there's some nice viral |
| "compress.php". That's what I'm naming mine. This | | | | promotion right there. |
| means the htaccess file you have should look as | | | | You could also adapt the script to check the HTTP |
| follows: | | | | referer (in the variable |
| AddHandler compress .html | | | | $_SERVER["HTTP_REFERER"]) to see if the image |
| AddHandler compress .htm | | | | was called offsite. If it was, the script would put the |
| AddHandler compress .txt | | | | watermark on there but if you called it from a page on |
| Action compress /compress/compress.php | | | | your own site, the image would be shown without one. |
| If our wrapper were simply going to pass through the | | | | |