Is it possible to override output with Apache/PHP

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
mjhamson
Forum Newbie
Posts: 6
Joined: Fri Dec 02, 2005 7:47 am

Is it possible to override output with Apache/PHP

Post by mjhamson »

Reference: here is a perfect example in the .NET world... of what i want to do in the PHP world.
http://www.codeproject.com/aspnet/sumit ... ntrols.asp



I come from the .NET world where it is really easy to create a template system that overrides the rendering of a page so you can infuse code into the requested page prior to the user seeing it.

Ex.
content-developer creates a page "foo.html". It is a basic content page with the basic HTML tags (META, BODY, ect). However, there is no code for menus, footers, headers, ect... just for the actual content.

When a user invokes the httprequest for the page http://www.fooserver.com/foo.html , (what i need) is to have the html page intercepted by php code that then infuses the footer, header and other branding/global information into the page before giving it off to the user.

The sticky here is that i need to do this without touching any apache settings... and the URIs should remain as static looking as possible.

I can change my .htaccess so i do have some flexibility.

I just do not have any idea of where to start from in the PHP/Apache world. So any pointers would be GREATLY appreciated.

-michael

gmail acct: mhamson
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

if possible, set an .htaccess to add handler html -> php (cant think of teh syntax off the top of my head) and then in the .html file, simply use php's include()

alternatively make the entire page as php, and include the pieces of HTML where desired

it's pretty easy actually - sort of a half-@$$'d template system :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You could set up your .htaccess file so that whenever Apache is asked for a .html file, instead it serves them up a PHP file. Then, that PHP file infuses the .html file into a PHP page complete with a header, footer etc. This can be done without changing the URL of the page. Here's an example I just cooked up:

Code: Select all

Options +FollowSymlinks
RewriteEngine on

#Any requested file that isn't index.php, forward to the index page
#The $_SERVER['REQUEST_URI'] remains unchanged - that can be used to 
#find the intended page

#just made this line up - it's supposed to forward 
#anything that is an html file, on to phpwrapper.php
RewriteCond %{REQUEST_FILENAME} ^.*\.html$

RewriteRule (.*) http://www.domain.ca/phpwrapper.php
Hopefully this helps.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Take it to the next level

use mod_rewrite (possible in htaccess) to rewrite your html files to handler.php?page=XXXXXXX, where XXXXXX is the name of the page. You can then have on master template file and pull content from any source (database, filesystem, xml feeds, etc..)

If you want to go this route let me know and I will elaborate further if you can't find anything on google

Edit: eh it's pretty similiar to what pickle suggested I guess now that I look again, but on his you're using the REQUEST_URI, I would have let the rewrite engine send the query right to the script
mjhamson
Forum Newbie
Posts: 6
Joined: Fri Dec 02, 2005 7:47 am

Post by mjhamson »

jshpro: if you could elaborate it would be great. So far what I am finding is that its not possible to do exactly what I can in .NET. Additionally, the thoughts of what I have been reading and seeing makes me think that it is barely a step up from using html includes.

I guess what I am trying to do is build a honest-to-goodness framework... something that is cheap to do in other worlds. :-(

I would also suspect that I should not need to worry about load thresholds when using the mod_rewrite since the server(s) will see less then a million hits a month?

-Michael
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

You may want to keep your HTML readable without having the extra processing, in that case, you might want to look into PHPTal (sorry, no link, google it).
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

In using mod_rewrite you can make it so

/file5.html

gets forwarded to

/handler.php?name=file&number=5


or something along those lines, you can make


/file/5.html

become

/handler.php?name=file5

or really anything that is possible with regular expressions

To the outside world (end users, and search engines) all they will see is the .html files, only you will know your "dirty" little secret (that you're really using PHP). Also PHP outputs an http header powerd-by:php or something, you can disable that in php.ini fairly easily, I think it's called expose php and it's just a boolean. I can assure you I understand what you're trying to do and PHP can do it. Whether or not you're willing to do the research involved is the only factor. Complete mod_rewrite documentation is on apache.org and all PHP syntax is covered on php.net
mjhamson
Forum Newbie
Posts: 6
Joined: Fri Dec 02, 2005 7:47 am

Post by mjhamson »

the PHPTal is looking very good... and being that I have dug into drupal a little bit... I think this might be a good avenue to take.

I really enjoy seeing the feedback and thoughts of the community so please don't stop. I will investigate this avenue first... but am always open to learning new means.

-Michael
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

By the way, since you're doing a mod_rewrite solution, you may want to drop the .html extensions in order to advance future compatibility. Make sure you send the text/html headers, etc, but if somewhere in the future a new document standard comes into play, you won't have to change all your extensions. This is especially applicable on scripting pages like .php, .cfs, .asp, and less so on .html and .css.
Post Reply