Global PHP Header Use
Green-Beast.com makes extensive use of PHP server-side includes. This technology is used throughout the site for nearly every grouping on every page. As an example, the Primary Navigation links which appear on every page is actually a single file which is included into the document body. The advantages may be clear to you already, but if not, they are…
- Saves time. I only had to write one file, one time, reducing my initial work load substantially.
- Increases site performance, reduces page-load time, and saves bandwidth consumption, once the file is cached.
- Makes updates and edits much faster and easier since only one file needs to be worked on. A huge plus.
Green-Beast.com also does this with the <head></head>
; a Global PHP Header for is included in its pages. To clarify, this means that instead of having the header information on each page, it is instead written only once. In the case of the header, I created a single header file I called header.php. In it I added the PHP cookie script, Document type (DOCTYPE or DTD), title, relationship and reversal links, hypertext mark-up language (HTML) equivalents, PHP style-changing script, and other meta data such as previous and next-page navigational links, description, and keywords.
Once that was done, all I had to do is “include” it at the top of each page, as follows:
<?php require("header.php"); ?>
<body>
Simple, right? It really is. But there are some big disadvantages such as…
- The title stays the same, instead of changing from page-to-page as you may want it to.
- Same goes for the site/page description and keywords.
- It makes it impossible to incorporate relationship navigation links detailing what page is the previous one and which one is next in your preferred sequence.
Well, this might be fine for some, but it’d be better to add some of these variables to the header. What’s a person supposed to do? Well, variables is the operative word. I first determined what the variables were and came up with: title, relationship navigation links (prev/next), description, and keywords. So, what I did is create a page-specific PHP file — pagename_metadata.php — for each of the pages put the variable data in it, as such…
<?
$title = "PAGE NAME";
$prevlink = "last_page.php";
$nextlink = "next_page.php";
$description = "A page-specific description went here";
$keywords = "comma, separated, keywords, went, here";
?>
When that was done, I had to allow inclusion of this variable data into the header itself. This was easy to do, like so…
<title>GLOBAL SITE NAME :: <?=$title ?> :: Other Global Title Information</title>
<link rel="prev" href="<?=$prevlink?>" />
<link rel="next" href="<?=$nextlink?>" />
<meta name="description" content="A site global description plus <?=$description?>" />
<meta name="keywords" content="Some, site, global, keywords, plus, <?=$keywords?>" />
I then changed the very top of each page body by adding this PHP to include the respective defined variable data page…
<?php require("pagename_metadata.php"); ?>
<?php require("header.php"); ?>
<body>
The results, combining that shown above, are as follows…
Title: GLOBAL STE NAME :: PAGE NAME :: Other Global Title Information
Prev: last_page.php
Next: next_page.php
Description: A site global description plus A page-specific description went here
Keywords: Some, site, global, keywords, plus, comma, separated, keywords, went, here
It works great, offering the advantages of the global includes, while still being able to incorporate some variables for each page. There are other ways to get this done, though, such as not including the unmodified global items in each page by just putting that data right in variables list. But if that were to be done, then adding in more script to cover bases, should a variable file not be available for some reason, would have to be done. Example, using the title element…
<title><?=$title ?></title>
Thus the variable listed would be complete like this…
<?
$title = "GLOBAL STE NAME :: PAGE NAME :: Other Global Title Information";
?>
But, as noted, if that page variable file were unavailable, the title bar would show nothing. (In fact, if it were gone as I require it as an include, you’d get a PHP error). This last part would be unavoidable and unworkable, but if it was a variable written on the top of each page body, then you could cover bases by making an if/else statement as shown…
<? if ($title) { ?>
<title>GLOBAL SITE NAME :: <?=$title ?></title>
<? } else { ?>
<title>GLOBAL STE NAME :: Other Global Title Information</title>
<? } ?>
Thus if the variable information was not available for some reason, the global site title information would still show up anyway, by default. I went about it a different way is all.
I would like to thank Simon Wiffen, Senior Web Designer of Sense UK for his helpful insights in the development of this strategy.
Michael responds:
Posted: April 13th, 2005 at 2:36 am →
Been doing this for a few years now. It does expedite page creation by a bit.
You could actually trim some code a bit, by using:
<title>GLOBAL SITE NAME :: <?= (isset($title) ? $title : ‘ Other Global Title Information’) ?></title>
Tyler Davis responds:
Posted: November 1st, 2005 at 5:59 am →
I like your blog. It is a very interesting one.
Jason Cole responds:
Posted: November 1st, 2005 at 11:47 am →
I’m asking myself: How can it be that I’ve never ran through your site before? It’s a great one!