process all page requests by one file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cesine
Forum Newbie
Posts: 2
Joined: Fri Nov 12, 2004 11:05 pm

process all page requests by one file

Post by cesine »

i'm not sure how to ask my question but here's a shot.

i want to process (i think all) requests to my server (apache) by a php file. the file would call to include a menu, a login, a comments file, and the file requested. i suppose one way to do this would be to have apache always redirect to this php file, passing in the requested file's name? i could do this with the missing file option

im looking for the easiest and least-silly way of doing this.. i could make all my pages redirect to the missing file.. and then have the missing file process the request.. but that seems silly

am I silly for wanting to do this.. whats a better way. i havent found many answers because i dont know what im searchign for? are people already doing this.. waht do they call it...
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

If i understand your question fully you want to load all pages on your site from one single page - i.e. all requests go to index.php, but depending on an added variable it will show a different page to the user. This is very common and very easy to do. There is lots of threads in the forum that discus doing this but as a quick tip...

page called: index.php?page=contact

Code: Select all

if(isset($_GET['page'])){
   include('some/path/'.$_GET['page'].'.php');
}else{
   include('some/path/home.php');
}
Then you will want to get more complex and check the file that the user is requesting to make sure its ok to show them it, and add more error checking etc. But that is the basic principle of it. Just include that code in the page template and then the page will always look the same - just with a different main content. You can then do the same with the menus if you wish.
cesine
Forum Newbie
Posts: 2
Joined: Fri Nov 12, 2004 11:05 pm

Post by cesine »

yes actually that is waht im doing now, but i dont have a way of sending all page requests, or file requests through my index.php should i just rename my files to .inc so they wont "exist" and then have all page not found errors handled by my index.php.. isnt there a smarter way of doing this? do i realy want to do this? what are reasons not to...
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

as said before, search the web for "front controller".

you could use apache's mod_rewrite to map all all request to a given page.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

You could add a RedirectMatch to the httpd.conf file.
Post Reply