This tutorial was made assuming that no or very limited knowledge of PHP is known by the reader and some knowledge in HTML. This technique is not hard to implement, easy actually, but quite hard to explain so bare with me here.
The main purpose of this tutorial is to show of an easy way for a person to create/manage a site easily without having to edit every page of the site if you change one thing. Mostly it has being solved by using CSS externally and then linking it on every page. But what if you need to change the CSS? What if the location of your external CSS file changes and you need to change 50+ pages? It'd take a long time wouldn't it? Well, this tutorial will help you fix that using PHP.
Well, you don't have to use PHP, for this you can use some other languages, but I only know of a sure way in PHP and I use it. Most of the other web developers/programmers use this technique as well and if you don't, you might want to start doing this to make it easier for you to make and manage your site much more easier. Certainly helpful.
You would learn, if you haven't already, how to include pages using PHP into other pages and how it would help you in creation of your site. The codes in PHP that you would learn/use in this tutorial would be how to store things into variables, using the "echo" command and how to include pages into other pages. They would all be explained on the tutorial as we go through them.
Testing if you have PHP Enabled
If you know you have PHP on, or if you're just reading this tutorial, you can skip this section.
Please take note that to test it you would either have to have wamp installed (or any other PHP enabled servers), or if you have an on-line server, make sure it has PHP enabled on it. If you don't know how, than make a file, save it as "test.php" and put the following code in it...
<?php phpinfo(); ?>
If you see tables filled with information on them, that you have PHP enabled, but if you see the line of code that you have written in on the test.php, than you don't have PHP enabled. If the test fails altogether, than change the code into the following.
<?php $test = 'You have PHP enabled'; echo $test; ?>
That's just another simple test for the same thing.
Saving your work
If you are making your site as you read the tutorial, I advice you to save the pages that you would include into other pages into a different directory. You can name the directory as "Includes". You don't have to capitalize the first letter, but I do that to save confusion later on in the coding.
The Configuration File
The main purpose of the configuration file is to set universal variables that would/could be used throughout the site, or portions of the site. It would help you later on with management of your site when you want to fix something that is used about in every page of your site. Such as the administrator e-mail. This file would be included into every other file in one line of code. How? Just read on and find out.
Mostly, the main variables could be the site name, site url, meta tags such as description or keywords if they would be the same throughout the site.
Setting the variables that would have things stored into them shouldn't have any dashes or anything in them. Even if the variable name would have two or more words in them. Just capitalize the first letter of each word to show the ending of one word and beginning of another word. This would save you from encountering PHP errors or anything like that.
It is best to put divisions in your configuration file if certain portions of the variables aren't for the whole site (but can be used throughout), but mainly aimed for a certain portion of the site, such as contact us page. You canalso nullify a variable from being usable if you know you will need that variable in the future but not now by putting two backslashes (//) in front of it like some variables in the example below ($adminEmail, $siteSlogan...).
Below is an example of a configuration file...
<?php //*************************************************************************// //** Configurations for the many additions and features of my site... **// //** ... also for better flexibility of my site and for easier editing. **// //*************************************************************************// //** Main site Configurations **// $siteName = 'MySite'; $title = 'Title for MySite'; //$siteSlogan = 'This is a slogan for MySite'; $metaDescription = 'MySite - Anyone can visit MySite because its MySite.'; $metaKeywords = 'MySite, My, Site'; $copyright = 'MySite © 2007-2008'; //** Contact Us Configurations **// //$contactTitle = 'Contact Us'; $adminEmail = 'MySiteAdmin at email.com'; ?>
The words you see after the dallor sign ($) but before the equal sign (=) are the variable names. They give the variable(s) the unique identifier that would let you recall them later on multiple times. This way, if you have more than one of the same text that would be the same text throughout the site and you would need to change them, you will change it in the configuration file and save it. It would change the rest throughout the site. Would save time and effort and make it simple and easy.
The configuration file would be included into the header page which would be included into the main content.
You may want to cut your site in pieces and save the universal peices into the Includes directory where you will include into the main content pages. This way, if the code for the header would need to be edited, you edit one file in the Includes pages and it would change all of the headers on the rest of the site. Easy and simple.
An image showing somewhat of an outline of what I'm talking about is shown below...
If we put our attention on one of the main content pages, here's how the diagram would look like...
In general, we are including main universal content that would be the same for all pages throughout to the content pages. The content page might look something like the following piece of code...
<?php include_once('includes/header1.php'); ?> <div id="wrap"> <?php include_once('includes/header2.php'); ?> <?php include_once('includes/leftContent.php'); ?> <div id="rightContent"> <div class="content"><p>Main Content</p></div> <div class="mainContent"> <p>Welcome to <?php echo $siteName; ?>! Have fun and enjoy being on <?php echo $siteName; ?>. If you have any questions please <a href="mailto:<?php echo $adminEmail; ?>">Email Us</a>. Thank you!</p> </div> <?php include_once('includes/footer.php'); ?>
An example of how a header.php might look like is...
<?php include_once('site_config.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title><?php echo $title; ?></title> <meta name="keywords" content="<?php echo $metaKeywords; ?>" /> <meta name="description" content="<?php echo $metaDescription; ?>" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="Scripts/design.css" /> </head> <body>
The could be more than one header on your site. The header that goes up to the "<body>" tag and the header that shows the main image of your site, or you can combine the two headers and make it as one header page. Or just have a header that goes up to the "<body>" tag.
Take notice, that all of the pages, including pages and the main content pages would be saved under the extension PHP and would need PHP enabled as was brought up in a prvious section of this tutorial.
In general, you are including universal files stored in a different Directory into main pages that would be publicly viewable which would make it easier for the web developer/programmer easily manage the site. This would be helpful if the site you are making is going to grow 50 pages and more.
Forums and other services like forums (such as blogs) use this technique to manage and create pages automatically.
This page was published on It was last revised on