Lets talk about managing a large site. If a change comes around, say a new company logo for example, or even a minute change in the main menu, normally you will have to go through each page and make the change manually. Now this can be very time consuming, especially if you have quite a few pages to deal with. The changes could literally take hours. Even with scripts or ‘search and replace’ programs, it’s a very tricky process.
Now lets talk about a more efficient way. You have a few files, that control the entire static look of the site ( such as the header, menu’s, footer, etc ). You make a change to one of those files, and it updates everything automatically. If you site is anything larger than a few pages, this is a very nice feature.
Lets get into a few examples on how to do this. There are a few ways. The 3 most common ways to build a dynamic site are:
1. Using SSI ( Server-side includes )
2. Using PHP includes
3. Pulling data from a database ( mySQL, etc )
The first two examples involve text files ( much like HTML files are text-based ). Each part of the page you wish do make dynamic ( menu’s, header, footer, etc ) is taken out of the html document, and put into separate text files. You can name the files anything you want. Common extensions are.txt and .inc. In place of the HTML code you removed, you put an “include” that points to that text file. I will use this page as an example.
This page, in all reality consists of just the META tags at the top, and these paragraphs of text you are reading now. The header on top ( logo, etc ), the footer below ( copyright info ) and the menus to the left and right are all pulled from text files.When you view the source of the page, you could never tell. That is because it is all called via PHP includes. PHP ( and SSI for that matter ) are done SERVER SIDE, meaning all processing of data is done at the server level before it reaches your browser.
A sample PHP include looks like this:
Please note, that in order to use PHP includes your file names have to end with .PHP or .PHP3.
With SSI, the include looks similar, however you can set it up to be used in just regular .HTML or .HTML files. Otherwise you will have to use .SHTML file extensions.
Calling SSI within a PHP document:
<? virtual(‘/server/path/to/script.cgi’); ?>
A normal SSI include in a regular HTML document:
<!–#include file=”myfile.html” –>
If the file your including is a script that needs to be executed:
<!–#exec cgi=”scriptname.cgi” –>
By using includes as stated above, you can build yourself a truly dynamic site. Make a change to 1 file, and all files are changed ( since they all call that 1 file you edited ).
The third example, involves using a script to make queries to your database. Since it varies so widely, I won’t be covering that topic here.