Can someone perhaps link me to a tutorial that shows how to make a master template page for the website. So all the pages will have the same css, banners, and menu links appearing and only the body content changes depending on the page. I have been trying to find a tutorial with basics to allow me to perform such a thing, but they do not really help.
Thank you very much
PHP master page
Moderator: General Moderators
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: PHP master page
This is a very vague subject that can be done a million different ways. An easy way is to use query strings and set up files in a folder like "/content". So, the links would look like example.com/?index or example.com/?members/view&id=1. This will direct all traffic through your index.php without using a .htaccess file. If you did use a .htaccess file, the only different would be that you'd remove the question mark and use it for query strings only.
Then, you'd store your content in your "/content" folder in the same structure that you want it to display. All of the content would be the items that exist in the content section of your pages and not the rest of the template. Then, you'd look at your query string to know where to go. Because it's possible to mess up your $_GET array like this (i.e. example.com/?member&member=1), it's a good idea to rebuild it. So, index.php would be like this:
[syntax=php]<?php include 'config.php'; // A file holding things like your site URL, database credentials or connection, etc.
$request = !empty($_GET) ? key($_GET) : 'index'; // Gets the first key of $_GET, if one exists. Defaults to 'index';
$_GET = parse_str(substr($_SERVER['QUERY_STRING'], strlen($request))); // Rebuilds $_GET array without request key
if (!file_exists(dirname(__FILE__) . "/content/$request.php")) {
$request = '404';
} ?><!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="container">
<div id="header">Header</div>
<ul id="nav"><li>Nav Item #1</li></ul>
<div id="content"><?php include "content/$request.php"; ?></div>
<div id="footer">Footer</div>
</div>
</body>
</html>[/syntax]
Note that this is untested.. I just wrote this. You may need to use strlen($request) - 1. Also, normally when I do things like this my code is much longer and more complex. I like to make use of the output buffer for things such as flags to replace the entire display with an error. I also make error messages and debug messages a part of my templates. And, in terms of security, you'll want to maker sure special strings such as ".." (i.e. example.com/?../index) are considered invalid and go to your 404 page. Never trust the user.
Then, you'd store your content in your "/content" folder in the same structure that you want it to display. All of the content would be the items that exist in the content section of your pages and not the rest of the template. Then, you'd look at your query string to know where to go. Because it's possible to mess up your $_GET array like this (i.e. example.com/?member&member=1), it's a good idea to rebuild it. So, index.php would be like this:
[syntax=php]<?php include 'config.php'; // A file holding things like your site URL, database credentials or connection, etc.
$request = !empty($_GET) ? key($_GET) : 'index'; // Gets the first key of $_GET, if one exists. Defaults to 'index';
$_GET = parse_str(substr($_SERVER['QUERY_STRING'], strlen($request))); // Rebuilds $_GET array without request key
if (!file_exists(dirname(__FILE__) . "/content/$request.php")) {
$request = '404';
} ?><!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="container">
<div id="header">Header</div>
<ul id="nav"><li>Nav Item #1</li></ul>
<div id="content"><?php include "content/$request.php"; ?></div>
<div id="footer">Footer</div>
</div>
</body>
</html>[/syntax]
Note that this is untested.. I just wrote this. You may need to use strlen($request) - 1. Also, normally when I do things like this my code is much longer and more complex. I like to make use of the output buffer for things such as flags to replace the entire display with an error. I also make error messages and debug messages a part of my templates. And, in terms of security, you'll want to maker sure special strings such as ".." (i.e. example.com/?../index) are considered invalid and go to your 404 page. Never trust the user.