Custom Page Titles with PHP Includes
The problem with using =$php;?> includes to make maintaining larger websites easier is it means sacrificing certain customisations (i.e. custom page headers) that would be possible with individual pages. As specific page titles are like extra keywords and can raise your page 'importance', not having them means sacrificing page rank too. Or so you might have thought! Fortunately there are a couple of methods to make customising page titles easy.
Method One
The first method, the one I've chosen for this website, involves naming your pages descriptive names and deriving the individual subtitles from this.
If you look at the name of this, you can see that it perfectly describes the page: php_custom_page_names.php. To turn this into a 'proper' title we'll need to strip out the extension (.php) and any special characters (in this case, underscores). Getting the page is easy with the SERVER superglobal, so let's grab it and assign it to the $page variable:
$page = basename($_SERVER['SCRIPT_FILENAME']);
Now to clean it up! We'll be using the str_replace() function to replace special characters and the redundant extension at the end:
$page = str_replace('_',' ',$page);
$page = str_replace('.php','',$page);
"$page" should now will now look like this: "php custom page names". You can tidy this up even further by capitalising the first letter of each word (for extra professionalism):
$page = ucfirst($page);
Altogether now, this looks like this:
<?
$page = basename($_SERVER['SCRIPT_FILENAME']);
$page = str_replace('_',' ',$page);
$page = str_replace('.php','',$page);
$page = ucfirst($page);
?>
Place the above coding anywhere in your header include file, before your <title> tag. Now, to actually implement it we simply echo the $page variable after our main title (and a separating character), like so:
<title>
MAIN TITLE » <? echo $page; ?>
</title>
Method Two
This second method is perhaps slightly quicker and probably ideal if you already have lots of pages (because changing page names breaks the web and is a bad idea!) This idea is the most simple of the two shown here.
Open the pages that you wish to have custom subtitles for, and before the header include is declared, add a variable called subtitle with your custom subtitle, like so:
<?
$subtitle = "custom subtitle";
include('header.php');
?>
Now you need to open the header include, and add a bit of fancy stuff. First, we need to check if the custom subtitle is set — echoing the $subtitle variable before it is set will spring up some errors. To check, we need an if statement:
if (isset($subtitle)) {
echo $subtitle;
}
Pretty simple when you read it to yourself in "structured English": if is set, echo subtitle. Of course, if you want a default subtitle for when a custom one is not set we need an if/else statement (although it's not necessary):
if (isset($subtitle)) {
echo $subtitle;
} else {
echo "default subtitle";
}
Now we just need to stick this in with the title in the header include (I've compressed it a little to save space:
<title>
MAIN TITLE » <? if (isset($subtitle)) { echo $subtitle; } ?>
</title>
Both simple, both good for your search engine ranking.
Tags:
php, seo, includes,
Last Updated On: 19th February 06 by Jem
Bookmark At: StumbleUpon, Digg

Handy Stuff
Downloads
Friends of 'TT'
Resources