HTACCESS Wrappers with PHP

Written by Robert Plank

HTACCESS is a remarkable tool you can use for password protection, error handling (like custom 404 pages), or HTTP redirects. It can also be used to transform whole folders in seconds: adding headers to all your HTML documents, watermarking all your images, and more.

A wrapper is like a middleman. Using htaccess you can tell your web server to "forward" certain files to PHP scripts of yours. When a visitor tries to load an image in their browser, you could activate a script that adds a watermark torepparttar image. When an HTML page is loaded you could query an IP-to-country database and have your HTML pages translated intorepparttar 107755 native language of your visitor's country-of-origin.

Every file in a folder, or all files of a certain type in a folder, can be instructed to go through a PHP script.


Pretend you host several affiliate sites, or a full-blown hosting service like Geocities. Most sites running on free hosting services have some kind of advertisementrepparttar 107756 owners use to generate revenue. These aren't applied voluntarily byrepparttar 107757 users of these services. The ads don't even show up on their source files, just when displayed onrepparttar 107758 web.

It's possible to replicate this feature using less than 10 lines of PHP and htaccess code. To start off, make a folder on your web host called "header". Create a new text file and enterrepparttar 107759 following:

AddHandler headered .htm AddHandler headered .html

Action headered /header/header.php

This designates files withrepparttar 107760 extension ".htm" and ".html" to a type called "headered". The name "headered" can really be anything, it's just a way of labeling a group of files. The last line there tellsrepparttar 107761 web server that if any ofrepparttar 107762 file types inrepparttar 107763 group called "headered" are called, we should instead executerepparttar 107764 script "/header/header.php". This isrepparttar 107765 relative path, so if your URL is, this will run

That's all you've got to do forrepparttar 107766 htaccess file. Save that as "htaccess.txt" -- we'll get back to it later.

Forrepparttar 107767 actual wrapper, create a new text file withrepparttar 107768 standard tags, then assign your header and footer file names to variables called $header and $footer.

$header = "header.html"; $footer = "footer.html";

Redirecting a user to our script doesn't pass its contents to it, justrepparttar 107769 filename. If you call phpinfo() in your script and scroll torepparttar 107770 bottom you can see allrepparttar 107771 server variables which give usrepparttar 107772 name. The element "REQUEST_URI" in $_SERVER gives usrepparttar 107773 relative path (/header/sample.html), but we wantrepparttar 107774 full system path since we're going to be readingrepparttar 107775 actual file (/home/username/wwwroot/, which is "PATH_TRANSLATED".


The name ofrepparttar 107776 file that just tried to be shown is now stored inrepparttar 107777 variable $file. Three simple things are left: outputrepparttar 107778 header, outputrepparttar 107779 actual file, then outputrepparttar 107780 footer.

readfile($header); readfile($file); readfile($footer);

That's it. Here'srepparttar 107781 entire header.php file:

$header = "header.html"; $footer = "footer.html";

$file = $_SERVER["PATH_TRANSLATED"]; readfile($header); readfile($file); readfile($footer);


All that, in just nine lines of code. Download it here: utorials/wrapper/

That containsrepparttar 107782 htaccess file and PHP wrapper script, along with a sample header, footer, and a test page. Upload all five files to your web host, chmod htaccess.txt to 0755 then rename it to ".htaccess". It might disappear from your directory listing which is okay, it should still be there.

Load, in your browser,repparttar 107783 copy of sample.html residing on your web server. The text "This is my header" should appear atrepparttar 107784 top while "This is my footer" should show onrepparttar 107785 bottom. If you open uprepparttar 107786 actual file called sample.html, you'll see that these actually aren't there. They've been added in byrepparttar 107787 script all HTML files inrepparttar 107788 folder "header" must now pass through.

This is how wrappers work. Certain things, like adding custom headers and footers are done "onrepparttar 107789 fly" without modifying your original file. You'll getrepparttar 107790 same effect if you create other HTML files and upload them to this folder.

Files without ".html" or ".htm" extensions, such as text files or images, won't show these headers or footers. This is a good thing because text files aren't part ofrepparttar 107791 presentation on a web site and adding extra text to images will corrupt them. It affects all HTML files within your /headers folder, and none ofrepparttar 107792 files outside of it.

If you wanted, you could add or remove any file extensions you want, just by adding or taking away those "AddHandler" lines.

To get everything back to normal, either delete your .htaccess file or upload a blank .htaccess file in that folder, and all will be well again.


The same basic formula can be applied again for other uses -- HTTP compression, for example. This was an idea that used to be impractical because computers ran at slower speeds, and is now obsolete because of broadband technologies (DSL and cable).

It works like this: when an HTML page is loaded,repparttar 107793 web server instead givesrepparttar 107794 visitor a zipped or compressed version of that page. The visitor downloads that file, which of course takes up less space thanrepparttar 107795 real thing and downloads in less time, then unzips it and displaysrepparttar 107796 original page.

In this age of lighting fast DSL lines, there's almost no noticeable difference. However, if you have a site that hosts large files whose audience is mostly dialup users, it might be something to look into.

Make a new folder called "compress". Create your htaccess file again, just as before, but setrepparttar 107797 extensions to include .htm, .html, and .txt. (The group name, folder name, and script name have nothing to do with one another, you can name any of these whatever you like -- I just like things to match.)

Our wrapper script for this should be called "compress.php". That's what I'm naming mine. This meansrepparttar 107798 htaccess file you have should look as follows:

AddHandler compress .html AddHandler compress .htm AddHandler compress .txt

Action compress /compress/compress.php

If our wrapper were simply going to pass throughrepparttar 107799 file (in other words, just read its contents into a variable and display it), our handler script would look like this:

Hard Drive Selection

Written by J A Carpunky

Perhapsrepparttar biggest mistake people make when building their computer(s) is to underestimaterepparttar 107754 importance ofrepparttar 107755 hard disk drive (HDD). In our modern society, bigger is synonymous with better, so when you go to buy a hard drive you look at how "big" it is, its data capacity. But is that all you should be considering when buying your hard drive?

Like every other component of your computer,repparttar 107756 HDD is far too complex a system to describe in any sort of detail here, but hopefully byrepparttar 107757 end of this article you will know what to look for when you go to buy your new hard drive.

There are 4 primary aspects to look at in a hard drive (in order of importance): 1. Rotational frequency and average seek time 2. Buffer size 3. Internal/external transfer rates 4. Capacity

Rotational frequency has become a large selling point (5400/7200/10000/15000RPM) recently, so you shouldn't have too much difficulty finding out whatrepparttar 107758 rotational frequency of your HDD is when you go to buy it. Now of courserepparttar 107759 faster it spins,repparttar 107760 faster it can read fromrepparttar 107761 disk (there are other advantages also), but be aware thatrepparttar 107762 faster it spins,repparttar 107763 more wear is put onrepparttar 107764 drive, andrepparttar 107765 more likely it is to encounter errors and malfunctions later in life. Hard drives are pretty muchrepparttar 107766 only systems within your computer that have moving parts (CD-ROM, floppy disk drives, and fans also have moving parts, but they're all cheap and if they break you won't lose allrepparttar 107767 data you've accumulated overrepparttar 107768 course of owning your computer, be sure to back up your data regularly!). If you know anything about physics or engineering, then you know that moving parts produce friction and wear, and a faster spinning platter (the part that holdsrepparttar 107769 data and spins) means more friction, and therefore more wear on your drive. Of courserepparttar 107770 faster it spinsrepparttar 107771 faster it can read data fromrepparttar 107772 platter. So what can you do? well again you backup your data as much as possible and you getrepparttar 107773 fastest spinning drive money can buy (which is 7200RPM for ATA devices (the sort you are buying) and 15000RPM for SCSI and some Serial ATA devices (you might get Serial ATA, that depends on your motherboard)). So look for a "7200RPM" label onrepparttar 107774 drive you're thinking of getting, it's important!

The "buffer" I'm referring to isrepparttar 107775 transfer buffer that temporarily stores data to read from or write torepparttar 107776 hard drive. The reason for this is thatrepparttar 107777 bus (data channel betweenrepparttar 107778 hard drive andrepparttar 107779 motherboard) can only handle so much information at a time, and it takes a long time forrepparttar 107780 data to be found and retrieved from a platter (random seek time), so it takes a big chunk ofrepparttar 107781 requested data and stores it in a very fast data buffer to be sent atrepparttar 107782 bus's convenience. So with a larger buffer you can queue more data for transfer overrepparttar 107783 bus without having to keep sending requests to slowly find more data fromrepparttar 107784 platters. Some newer drives come with as much as 8MB of buffer capacity, but it is more common to see 2 or 4MB. Usually a drive with a large amount will use that as a selling point and print it onrepparttar 107785 box/website, otherwise you can assume it has a 2/4MB buffer. My advice is to find one with as much as you can afford.

Cont'd on page 2 ==> © 2005
Terms of Use