PHP HelpPHP Help

Online Community that helps beginners learn PHP,
and webmasters solve PHP coding problems

since 1999





Making the most of Site-wide Headers and Footers

Separating Content From Markup

Pretty simple, eh? OK, now we're going to get a bit more complicated. Say you want to be able to change the values of the dynamic content across your entire site very rapidly. The easiest way to do this is to move the information into a global configuration file, and store page-specific portions of it in an array. For those new to programming, any array is simply a list that can be accessed through either a numeric or a textual key. An example of a config file follows:

config.php

<?PHP $aryConfig = array(
"main" => array (
"title" => "My Cool First Page" ,
"caption" => "Welcome to the Coolest Site Ever" ,
"author" => "The Author" ,
"contact" => "me@coolsite.loc" ) ,
"second" => array (
"title" => "My Cool Second Page" ,
"caption" => "More Coolness!" ,
"author" => "The Other Author" ,
"contact" => "other@coolsite.loc" )
);
?>
 

The configuration file is inlcuded just prior to including your header. This makes the values if all the oncfiguration variables available to code througout the entirie scope of the page. Now that the config file is in place, the header and footer need to be modified to read the array We still want to be able to override the global values locally so we will make sure to only use the global values if there is no existing value. This is done as follows:

header3.php

<?PHP
if (empty ($strTitle)) { $strTitle $aryConfig[$strPagename]["title"]; }
if (empty (
$strCaption)) { $strCaption =
$aryConfig[$strPagename]["caption"]; }
?>
<HEAD>
<TITLE><?php print $strTitle?></TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<H1><?php print $strCaption?></H1>
 

footer3.php

<?PHP
if (empty ($strAuthor)) { $strAuthor $aryConfig[$strPagename]["author"] } ;
if (empty (
$strContact)) { $strContact $aryConfig[$strPagename]["contact"] } ;
?>
<PRE>
</PRE<
<CENTER>
<A HREF="http://www.coolsite.loc/legal.php">Copyright</A> 1999 By Me<BR>
Written by: <I><?php print $strAuthor?></I><BR>
Questions? Write to <B><?php print $strContact?></B><BR>
</CENTER>
</BODY>
 

And finally, the main page needs to be updated to access the config file as follows:

main3.php

<?PHP
$strPagename 
"main";
require (
"/path/to/includes/config.php");
require (
"/path/to/includes/header3.php");
?>
<P>This is a the text of your page. Make it as fancy as you want!</P>
<?PHP
require ("/path/to/includes/footer3.php");
?>
 

As an additional step, it may be easier for you to move the actual page content to a separate file. This will allow easy content changes without worrying about messing up the PHP code. This requires a change to the main file and the addition of a contents file:

main3.php

<?PHP
$strPagename 
"main";
require (
"/path/to/includes/header3.php");
require (
"/path/to/includes/contents.html");
require (
"/path/to/includes/footer3.php");
?>
 

contents.php

<P>This is a the text of your page. Make it as fancy as you want!</P>
 

This should be a good starting point for your forays into the jungle of dynamic site building. Have fun!

Tip:  You may be wondering how to make your index or home page load automatically now that it is dynamic. On the Apache web server, this is as simple as creating an .htaccess file in the directory containing a line including a DirectoryIndex directive that tells the server what files to consider "default" pages. You want to add the name of your PHP enabled home page to the list as follows. Make sure you put it first in the list to give it priority over other default pages but also keep at least one static default page in there for safety.

.htaccess

DirectoryIndex index.php index.html
 
Pages:  1   2   3Related Forum Topics   |   More Tutorials »




Copyright © 2017 PHPHelp.com