how to: (copy this site)
Updated: April 13, 2001
This guide explains how this site is set up. This is mostly a reference for myself because I tend to forget how I got something to work and when I need to change/add/fix it later, I have to spend hours trying to remember how it was done. So read on if you're interested or die now.
Introduction (To
Mackin')
The majority of the pages use
PHP. Go to the PHP site
for more info than your brain can handle. The reason
PHP is used is because it
makes maintenance of the site easier. For example, the navigation list is
contained in a single file. If I need to add or delete a link from the list I
don't need to edit every page that uses the list, I just edit the one file. I am
not the Wile E. Coyote of
PHP so some of my code could be sloppy, technically wrong, slanderous, etc.
but it all works the way I want it to, so who cares. The only "training"
I've had for PHP is from the
PHP site,
DevShed
and Google Groups. If you are
a PHP wizard and have a
suggestion to improve my code,
email me.
Codin' (Like A G.O.)
The various pages use the
include function to display the navigation list and the other parts of the
page that make up the site's template (i.e. date cell, time cell, copyright
cell, etc). This works OK but if I ever wanted to change the entire site layout
(e.g. move the date cell to the time cell and vice-versa) I would have to edit
every page to change the position of the
include tags. The solution I used for this was to pass everything through a
single file (/index.php shown here) which outputs the appropriate data. In
index.php the following function is in the table cell where the text you are currently reading is
printed:
| <? if (!$docid) { $docid = "news"; } // process the doc request include ("$showdoc[$docid]"); ?> |
The $docid variable is determined at the top of index.php (through a call to another PHP script) with the following function:
| if ($QUERY_STRING == "") { $docid = "news"; } include($_SERVER['DOCUMENT_ROOT']."/loftemplates/docs.php"); |
The value $QUERY_STRING is an Apache environment variable. If you go to a website and the address is something like http://www.187corp.com/index.php?docid=links the value(s) after the ? represent a $QUERY_STRING. As shown above in Code Sample 2, if $QUERY_STRING is empty (i.e. it equals "") the variable $docid is created and set to equal news. If $QUERY_STRING is any value other than "", the variable $docid is created and set to equal $QUERY_STRING (i.e. if the url is "http://www.187corp.com/index.php?docid=links" $docid is set to equal links). The $docid variable is looked up in an array table in docs.php which contains a list of keys and their matching values. The value of the variable $docid determines what content will be displayed (i.e. bio, vehicle, computers, links, etc.). As shown above in Code Sample 1, if $docid is equal to news, news.php is displayed. If $docid is equal to anything other than news (i.e. links) that value is looked up as a key in an array table in docs.php and the corresponding value ($DOCUMENT_ROOT/links.php) is included. The title above the content cell and the picture for the page are determined through the same process. The index.php file (shown here) contains the various include functions which call the other PHP scripts that make up the template for the entire site.
The problem with this setup is that someone can type in an address directly (i.e. http://www.187corp.com/links.php) and go directly to that page which bypasses index.php and therefore does not get formatted properly so it looks like crap (go here to see what it would look like). To stop this from happening I included the following script at the top of every content page on the site:
| <? if ($REQUEST_URI == "") { $QUERY_STRING = "news"; } $fixpage = ($_SERVER['DOCUMENT_ROOT']."" . "$REQUEST_URI"); include($_SERVER['DOCUMENT_ROOT']."/loftemplates/docs.php"); $redir = array_search($fixpage, $showdoc); //redirect to proper page if (in_array("$fixpage", $showdoc)) { Header("Location: /index.php?docid=$redir"); } ?> |
The variable $fixpage is created and combines the Apache $DOCUMENT_ROOT variable with $REQUEST_URI (which will equal what was typed). The resulting $DOCUMENT_ROOT/$REQUEST_URI is looked up in the array from docs.php and if there's a match, it's redirected to the proper page. If there's no match then something completely retarded was typed and you're getting a 404. So instead of going directly to http://www.187corp.com/links.php, the browser is automatically re-directed to the proper page (i.e. http://www.187corp.com/index.php?docid=links) and receives the proper formatting. If that makes no sense, too bad. I can't figure out how to word it differently right now.
Drawbacks
The only drawbacks that I've found so far with this coding are
a couple of things that need to be remembered. The following links would be
valid to the links page of this
site:
| 1. http://www.187corp.com/index.php?docid=links - proper link 2. http://www.187corp.com/links.php - re-directs to link in 1 |
A link of http://www.187corp.com/index.php?docid=links.php is not valid and will return a 404 error. This is because index.php will process the $QUERY_STRING (in this case links.php) and will try to find links.php in the array table which is a non-existent key. A function could take care of this but it would just make the files even more complicated and it isn't that hard to remember. Besides, no one links to my page so this doesn't really matter and I only wrote that crap to remind for when I can't figure out why my links are bad. Now die.
|
<? include($_SERVER['DOCUMENT_ROOT']."/loftemplates/headinfo.php"); ?> <div> <table width="100%" border="0" cellspacing="0"> <tr> <td class="onesmall" width="109" height="38" align="right" valign="top"> <? include($_SERVER['DOCUMENT_ROOT']."/loftemplates/topleft.php"); ?> </td> <td class="twobiggest" height="38" align="left" valign="bottom"> <? include($_SERVER['DOCUMENT_ROOT']."/loftemplates/title.php"); ?> </td> <td class="twosmall" width="260" height="38" align="right" valign="bottom"> <? include($_SERVER['DOCUMENT_ROOT']."/loftemplates/date.php"); ?> </td> </tr> <tr> <td class="onesmall" width="109" height="18"> <? include($_SERVER['DOCUMENT_ROOT']."/loftemplates/navtitle.php"); ?> </td> <td class="onemed" height="18"> <? include($_SERVER['DOCUMENT_ROOT']."/loftemplates/description.php"); ?> </td> <td class="onesmall" width="260" height="18" align="right"> <? include($_SERVER['DOCUMENT_ROOT']."/loftemplates/time.php"); ?> </td> </tr> </table> <table width="100%" border="0" cellspacing="0"> <tr> <td class="onemed" width="109" height="596" align="right" valign="top"> <? include($_SERVER['DOCUMENT_ROOT']."/loftemplates/nav.php"); ?> </td> <td class="onemed" width="11" height="574" valign="top"> <? include ($_SERVER['DOCUMENT_ROOT']."/loftemplates/pointer.php"); ?> </td> <td class="twomed" height="574" valign="top"> <? // Go to the default page if none requested if (!$docid) { $docid = "news"; } // process the doc request include ("$showdoc[$docid]"); ?> </td> <td class="twosmall" width="200" height="572" valign="top" align="right"> <? include($_SERVER['DOCUMENT_ROOT']."/loftemplates/piccell.php"); ?> </td> </tr> </table> <table width="100%" border="0" cellspacing="0"> <tr> <td class="onesmallest" width="109"> <? include($_SERVER['DOCUMENT_ROOT']."/loftemplates/copyright.php"); ?> </td> <td class="onesmallest"> <? include($_SERVER['DOCUMENT_ROOT']."/loftemplates/contact.php"); ?> </td> <td class="onesmallest" width="260"> <? include($_SERVER['DOCUMENT_ROOT']."/loftemplates/lofarmy.php"); ?> </td> </tr> </table> <? $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); printf(" <p align=\"center\"><font class=\"smallest\">This page was spit out in %f seconds</font></p>\n", $totaltime); ?> </div> </body> </html> |