ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

.htaccess Wrappers with PHP

(Page 2 out of 6)

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 advertisement the owners use to generate revenue. These aren't applied voluntarily by the users of these services. The ads don't even show up on their source files, just when displayed on the 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 enter the following:

AddHandler headered .htm
AddHandler headered .html

Action headered /header/header.php

This designates files with the 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 tells the web server that if any of the file types in the group called "headered" are called, we should instead execute the script "/header/header.php". This is the relative path, so if your URL is http://your.host, this will run http://your.host/header/header.php.

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

For the actual wrapper, create a new text file with the 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, just the filename. If you call phpinfo() in your script and scroll to the bottom you can see all the server variables which give us the name. The element "REQUEST_URI" in $_SERVER gives us the relative path (/header/sample.html), but we want the full system path since we're going to be reading the actual file (/home/username/wwwroot/your.host/header/sample.html), which is "PATH_TRANSLATED".

$file = $_SERVER["PATH_TRANSLATED"];

The name of the file that just tried to be shown is now stored in the variable $file. Three simple things are left: output the header, output the actual file, then output the footer.

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

That's it. All that, in just nine lines of code. Download it here: http://www.jumpx.com/tutorials/wrapper/header.zip

That contains the 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, the copy of sample.html residing on your web server. The text "This is my header" should appear at the top while "This is my footer" should show on the bottom. If you open up the actual file called sample.html, you'll see that these actually aren't there. They've been added in by the script all HTML files in the folder "header" must now pass through.

This is how wrappers work. Certain things, like adding custom headers and footers are done "on the fly" without modifying your original file. You'll get the 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 of the 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 of the 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.

« Previous: Introduction
Next: Shrink-Wrap »



Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction
  2. Tortilla Wrap
  3. Shrink-Wrap
  4. "Gift Wrapping" Your Output
  5. The best thing since bubble wrap
  6. The Wrap-Up
Bookmark Article
Download Article
PDF
Download this article as a PDF file