PHP Header Include – Saving development time

I have had the opportunity to go through a lot of other developers coding skills and practices in the past few months and I must admit there is a lot of time wasting going on. Are you the type of developer that has a <title>, <meta>, <link> and <body> tag on every page without a global include? If so, let me show you how to simplify your life.

I have had the opportunity to go through a lot of other developers coding skills and practices in the past few months and I must admit there is a lot of time wasting going on. Are you the type of developer that has a <title>, <meta>, <link> and <body> tag on every page without a global include? If so, let me show you how to simplify your life.

By having a global header include you can set all your title tags in one central location as well as control which page gets what header information all in one file.

Firstly, create a file called header.php and use the below template:

header.php

<?php
	// Get the filename of the current page;
	$cpage = substr(strrchr($_SERVER['PHP_SELF'], "/"), 1);
 
	// set this to a "default" title as a backup incase you forget to mention a page. This stops it from returning nothing.
	$wtitle = "My Default Website Title";
 
	// Control page titles
	switch ($cpage) {
		case "index.php": $wtitle = "Home page title"; break;
		case "contact_us.php": $wtitle = "Contact us"; break;
		case "products.php": $wtitle = "Products Page"; break;
		case "about_us.php": $wtitle = "About us"; break;
	}
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
  	<title><?php echo $wtitle; ?></title>
  	<meta name="robots" content="index,follow,all" />
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 
	<link rel="stylesheet" href="css/style.css" type="text/css" />
	<?php
		// if you're on the products page, load the products.css file.
		if ($cpage == "products.php") { echo '<link rel="stylesheet" href="css/products.css" type="text/css" />'; }
	?>
</head>
 
 
<body>
 
 
<div id="wrapper">

Now you can start creating all your other pages and simply include the header.php file right at the top as follows:

index.php / contact_us.php / products.php / about_us.php

<?php include "header.php"; ?>
 
<div id="content">
<p>Content goes here</p>
</div>
 
<?php include "footer.php"; ?>

As you can see we have taken this one step further and also created a footer.php file. This comes in very handy when you need to put tracking code at the bottom of ever page. Instead of pasting it on every single file, simply paste it into the footer.php.

footer.php

</div>
 
</body>
</html>

I hope you find this as valuable as I do. If you do it differently please give us the details. After all, we all learn from each other.

Who are you, really?

With everyone blogging these days it’s hard to find time to be original and come up with fresh content. I have created this blog in the hopes of not only documenting my personal projects, but to also share with others who are just as ambitious and passionate as I am when it comes to the extremities and complexities of the internet driven world we live in. So without rambling on too much let me introduce myself.

With everyone blogging these days it’s hard to find time to be original and come up with fresh content. I have created this blog in the hopes of not only documenting my personal projects, but to also share with others who are just as ambitious and passionate as I am when it comes to the extremities and complexities of the internet driven world we live in. So without rambling on too much let me introduce myself.

My name is Nick Duncan, age 25 and I (cl)aim to be a web entrepreneur by night, and a search engine optimisation consultant come advanced php web developer by day. Now that doesn’t sound like I have too much behind my name other than just a bunch of fancy words but I tend to think otherwise! I have 2 step-kids and a baby boy (Logan) that’s about to be born and take over the world. All originating from a woman that is super-hot and as loving and caring as an angel. Lucky, aren’t I?

I started web design from an early age of around 13, just a while after the PC game Interstate 76 was launched in S.A. I remember altering the game to make my car invincible with an extra gun and still make it fly, all of which was non-standard, of course! Soon after that I was into mIRC and quickly learnt I could make my own scripts within the program itself – BANG – All-Out-War was born, a text-based gangster game that seemed to have taken off like wildfire. The game reached the point whereby it hosted 100 players at any given time (after 4 months of solid development). By the time I reached 18 there was no time for fun and games any more, I had to go to work like everyone else. I landed up joining a foundry and started right at the bottom. I moved my way up through the company and in less than 3 years I received 4 promotions. I then bounced around different areas of the steel game until I realized how unhappy I was. I decided enough is enough and followed my heart, joining JD Internet Consulting who gave me the chance I needed. That was a mere 3 months ago and I have achieved so much in such a short space of time. It’s amazing how your potential can be unlocked by doing what you’re passionate about.

I wish you a warm welcome to my world and hope you enjoy the ride… Remember to keep both arms and legs inside at all times, and never, ever look down!