Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP's auto_prepend_file

Why is this absoutely revolutionary feature so little used/discussed?



10:25 pm on Jun 7, 2002 (gmt 0)

10+ Year Member

Hi: I'm new here.

I'm intruiged as to why PHP's auto_prepend_file and auto_append_file features are so rarely found. They would rank top of my list of stuff I wish I'd known about before, as they save massive amounts of work.

For those who have not used these, they automatically insert files you specify before and after the file requested by the site visitor. In other words, you don't have to continually faff about putting includes at the beginning and end of every file.

(You can exclude areas by using php_value auto_prepend_file none for sections of your site not wanted to be in the house style.)

In other words, the template for a page which should come out in the house style can be a completely blank (i.e. 0 byte) file.

Furthermore, combined with

$location = explode("/",$_SERVER[REQUEST_URI]);
array_pop ($location);
[and a small amount of further processing]

the likes of submenus and You are in: lines with links can automatically be inserted by translating the subdirectories in site.com/path/to/file.html into useful text. Obviously, this assumes your site has a well-structured hierarchy, but in my view any (large) site ought to for a variety of reasons.

Why is this so rarely encountered? Is it simply that no-one knows about it?

Is anyone aware of equivalents in other web-orientated languages?


12:09 am on Jun 8, 2002 (gmt 0)

10+ Year Member

I totally agree with you mvl22, although i use ColdFusion, but it's the same, nobody seems to use it(speed issues they say, but I ran some test and with careful programming, it's possible to get it to run faster compared to includes). It's basically the same concept: you have application.cfm running first, then your content file and then OnRequestEnd.cfm finish the job. I'm not too familiar with how it works with PHP, but in Coldfusion it's setup like this:

if you have your application.cfm and OnRequestEnd.cfm in your webroot, those files cascade to every directory below the root. So the logic in those files apply to every page in your domain, no matter how deep your directory structure is.

Now if you want a different layout for a specific directory, simply drop different application.cfm and OnRequestEnd.cfm in that specific directory and voila ;)

But when I work with PHP and Apache, I use mod_rewrite and a single logic page wich pull everything from MySQL, but that's another story (just sticky me if your interested)!



1:05 am on Jun 8, 2002 (gmt 0)

WebmasterWorld Senior Member marcia is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Great topic mvl22, thanks for bringing it up.

mavherick>>when I work with PHP and Apache, I use mod_rewrite and a single logic page wich pull everything from MySQL, but that's another story<<

This sounds incredibly useful, can we get some more discussion on it here?.


7:39 am on Jun 8, 2002 (gmt 0)

10+ Year Member

It's the least I can do!! I've been lurking for so long, I almost feel like an information thief :)

Before I continue, I would like to say that this is a mixture of methodology I came across, like Fusebox, Freenergy and such content and application management. I simply blended what I liked together and reworked some parts to accommodate my needs.

I'll start with a really basic example just to give you an idea (Don't be shy, I'm sure there is flaws with that setup so feel free to point the problems out).

Ok so you got your domain hosted with PHP, MySQL and mod_rewrite available. Now your company sells blue and red widgets, so your available pages could look like this:


So basically, I'm saying that you have a bunch of pages available in your root, blue-widgets and red-widgets directories but they don't physically exist as is. Let me explain.

It's where mod_rewrite comes into play. I'll assume you can setup rewrite rules and such (if not, you can find all you need using the search at the top of this page). Every request gets rewritten to a script I decided to call (no particular reason) fetch-page-data.php (for example: www.mycompany.com/blue-widgets/features.html translate to a request for www.mycompany.com/fetch-page-data.php?fp=/blue-widgets/features.html).

Our next step is to consider what our fetch-page-data.php will do. That's where I think all this start to make some sense (hopefully :)). For our example, this script's first task is to query our database using the variable fp. Let's pause for a second and talk about our database schema.

A really stripped down version again, but let's say we simply have a page table with fields like title, meta-keywords, layout-file, header-file, menu-file, content-file (you get the idea) just to name a few and most importantly file-path. (Which is used to get a match from our fetch-page-data.php script)

Now that our script located the data for a specific page, it simply includes the specific layout file using the layout-file from the record set. (Then again you could take care of actions prior to this step, like saving some customer info or updating session variables and such). The layout file then include whatever it needs like meta keywords, menus and content according to specific look and feel.

If no match is found in the database, simply send a custom 404 and you're set.

This is getting quite long, but I just wanted to specify that you'll have to set up some sort of include folder where all your layout, menus and content files will be.

Hope this is useful.



6:47 pm on Jun 8, 2002 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Why is this so rarely encountered? Is it simply that no-one knows about it?

It's such a transparent tool - meaning that once you've enabled it you hardly know it's there - and I'm afraid I'd forget it was if I ever needed to change something!! :)

But seriously, it is a handy tool that I should make more use of myself. Thanks for reminding me.


Featured Threads

Hot Threads This Week

Hot Threads This Month