/articles/dynamism last modified 37.996 days ago

Dynamism: Creating Dynamic Content with PHP

Using a scripting language to manage website content has advantages ranging from ease of management to functionality. My website has a lot of code behind it to manage the various pages listed in the navigation. Most professional websites employ a programming language to display headers and footers on every page with content, creating an easy to navigate and attractive website with little maintenance. This guide will walk through the steps of using PHP to generate dynamic content in two approaches.

Header and footer files

The first method up for discussion is the easier of the two. It involves creating header and footer files and using a PHP function call to include them both on each page. This method is secure, simple, and reasonably flexible.

header.php

Step 1: Create header file. This file will be included at the top of every page and should include proper HTML with a <head> tag and an opening <body> tag.

<html>
<head>
<title>My Page</title>
<style type="text/css">
<!--
body {
    background-color: white;
    color: #333333;
    font-family: verdana;
    font-size: 11px;
}
-->
</style>
</head>
<body>

footer.php

Step 2: Create footer file. This file will be included at the bottom, below the content. It should close the <body> and <html> tags. Obviously it can include any other information you would like to include at the bottom of every page (such as copyright information).

</body>
</html>

mypage.php

Step 3: Create a content page. Note that you will need PHP installed for this to work. You will know if you do not have it installed. This file can contain both PHP and HTML as normal, but at the top and bottom, insert the appropriate include() calls.

<?php include("header.php"); ?>
<h1>Hello!</h1>
<p>This is some content. Above you should see the header, and below you
should see the footer.</p>
<p>This is another paragraph.</p>
<?php include("footer.php"); ?>

Visit http://yourwebsite/mypage.php in a browser. If you created the files correctly and they are all in the same directory, you should now see the content of all three files together. The PHP include() call does what the name suggests: it includes the content of the given file. It will also evaluate any PHP content in the included files, allowing you to use other PHP code in the header and footer to perform other tasks. If you would rather not have the files evaluated, you can use readfile("file.php"); instead. The readfile() call simply returns the contents of the given file, without evaluating.

With this structure, uniform content can be easily achieved. Since the header and footer files are included dynamically, only those two files need to be changed in order to modify the appearance of the entire site automagically. However, such simplicity comes with a cost. Using this approach will restrict the title of every page to the static value set in the header, and, though of much lesser importance, the include() calls must be used on every page for which they are to be used.

For solutions to these problems, we can turn to a slightly more complicated approach which uses a central script to control the content. This basic idea behind this approach is what is used for my website.

Using a central script

This method will use a central script (index.php) to output all of the content of the website. The setup is similar to the method described above, except that a single script will control the inclusion of the header and footer instead of the individual pages.

header.php

Step 1: Create header and footer files just like above, but use <?=$title ?> for the title. This will return the value of the title variable set by the central script.

<html>
<head>
<title><?=$title ?></title>
<style type="text/css">
<!--
body {
    background-color: white;
    color: #333333;
    font-family: verdana;
    font-size: 11px;
}
-->
</style>
</head>
<body>

footer.php

</body>
</html>

main.php

Step 2: Create main.php, the default page. This page will show when no other page is specified.

<h1>hello!</h1>
This is the default page!

index.php

Step 3: Create index.php, the central control script. Read the comments (lines beginning with //) for explanations. Comments are ignored by the PHP parser.

<?php
// Assign the requested page to a shorter variable for simplicity.
// The value of $_GET["page"] will be obtained from index.php?page=RIGHT_HERE
$p = $_GET["page"];
// Check for bad requests. This is necessary to prevent the wrong files
// from being included. This is a security measure.
if (strpos($p,"..")) {
    // A bad request has been made, exit immediately. Do not proceed
    // with the inclusion.
    //  strpos($p,"..") checks for presence of ".." in the page request.
    //   ".." can be used to access files in parent directories
    die("Bad page request");
}
// File to include.
// this will be something like '/www/page.php'. If $p is empty, fall back
// main.php. THIS FILE MUST EXIST, otherwise bad things will happen.
if (!$p) $p = "main";
$content_file=$_SERVER["DOCUMENT_ROOT"]."/".$p.".php";
// See if $content_file exists. If it does not, redirect to the homepage.
if (!file_exists($content_file)) {
    header("Location: {$_SERVER["PHP_SELF"]}");
    exit();
}
// At this point everything is fine. Set the appropriate title and then
// include the files.
// Syntax: $variable = (condition) ? value_if_true : value_if_false;
$title = ($p) ? "$p - My Site!" : "Main page - My Site!";
include("header.php");
include($content_file);
include("footer.php");
?>

Visit http://yoursite/index.php in your browser. You should see the contents of the header, main.php, and footer. main.php is visible because no other page was specified. Now, create another file to represent another section of your website.

links.php

<p>
This is my links page. Notice there is no header or footer inclusion in this
file. There are no links here at the moment.
</p>

Save links.php with in the directory with index.php, header.php, main.php, and footer.php. Now visit http://yoursite/index.php?page=links or http://yoursite/?page=links (this will work if index.php is set in the DirectoryIndex directive of your Apache configuration). If done correctly, you should now be looking at the contents of links.php combined with the header and footer. Also, links - My Site! should now be the title of the page.

Clearly, this method is not perfect, either. The method you choose depends on what you need to do and how much effort you wish to put into the backend of your website. If you end up using the central script method described here, consider learning about mod_rewrite.