PHP Includes
When people say they're converting their site "into php", they mean that they've learnt how to use something called includes.
No, these people do not "know PHP" because they've learnt how to use one function, so please, if you learn how to use includes from this tutorial - or any other tutorial - don't go around saying you know PHP because you don't.
Now that's over with...
"Include" is a function in PHP which basically allows you to have the content of one file show up in any other file(s). They're very easy to use. :)
Let's say we have two pages. page "A" (index page) and page "B" ("About" page). (These pages are badly coded and missing a few tags. I sincerely hope your pages look nothing like these.)
Open both page A and page B into new tabs (or windows if you're using an *old skool* browser ;)) and look at the coding.
There are 28 lines of coding on page A. Out of all those lines, only 5 of them are unique to the page.
There are actually three parts to every page: header, content and footer.
(I'll be talking about page A in the following paragraphs.)
The header will contain everything up to the content; from the opening "<html>" tag (or the <!DOCTYPE..., if you're doing things correctly, unlike my examples ;)), all the way down to the line which has "<h1>Site Name</h1>" on it.
The content is everything that will be unique to that one page. This will be from the line which has "<p>Blah blah blah, welcome to my site...</p>" on it, all the way down to the line which has the second "<p>Some information</p>" on.
Everything else is the footer: from where we end the content div, all the way down to the closing "</html>" tag.
So, let's split page A into the three parts.
Header
<html>
<head>
<title>Site Title</title>
<link rel="stylesheet" type="text/css" href="/style.css">
</head>
<body>
<div id="container">
<div id="content">
<h1>Site Name</h1>
Content
<p>Blah blah blah, welcome to my site...</p>
<h2>Header...</h2>
<p>Some information</p>
<h2>Header...</h2>
<p>Some information</p>
Footer
</div>
<div id="sidebar">
<h2>Content</h2>
<ul>
<li><a href="/">link</a></li>
<li><a href="/">link</a></li>
<li><a href="/">link</a></li>
</ul>
<h2>Stuff</h2>
<p>other crap...</p>
</div>
</div>
</body>
</html>
Now you know how to split your pages up into three parts.
With this new knowledge, pull out the header part of one of your pages and put it in a new file. Name this file "header.php". Then pull out the footer part of one page and place it in another new file. Name this file "footer.php".
Place both files into the root directory (public_html) of your site.
After you've done this, go through each and every page on your site. Replace the header part of your pages with this code:
<?php
$rootpath = trim(strip_tags($_SERVER['DOCUMENT_ROOT']));
@include("{$rootpath}/header.php");
?>
And replace the footer part of your pages with this code:
<?php @include("{$rootpath}/footer.php");?>
(I'll explain exactly what these codes do at the bottom of this page. They are different to the include "codes" most people will give you.)
As you're replacing header and footer parts of your site with header and footer include codes, you'll need to rename your files to have a ".php" extension instead of the usual ".htm" / ".html" extensions.
You will also need to change all internal links from "[pagename].htm" / "[pagename].html" to "[pagename].php"...this can be done with a free and easy to use tool: TexRep^.
Once you've done all of this, you should end up with something like page "C". (Header code, content, footer code.)
You probably want to know exactly what the following codes do.
<?php
$rootpath = trim(strip_tags($_SERVER['DOCUMENT_ROOT']));
@include("{$rootpath}/header.php");
@include("{$rootpath}/footer.php");?>
(<?php "opens" php and ?> "closes" it...a bit like HTML tags in that, if you open one, it'll need to be closed.)
$rootpath = trim(strip_tags($_SERVER['DOCUMENT_ROOT']));
Most people will give you a generic server path which looks something like "/home/USER/public_html/" and they'll tell you to find out the username (USER) and then configure the path to your header.php file.
This means that, after setting up your site in includes, if you need to move to a new server and you're given a new server path, you're going to have to go through all of your pages (AGAIN) to configure the path to the new one. And I can tell you: doing this is a major pain in the backside.
So, how about we skip all of that completely and make the server give me the server path, up to my root directory?
This will require no brains or thinking on your part: it's nice and easy and if you ever move servers and end up with a different server path, you won't need to go through this again. YAY!
You should be aware though that it can be easily spoofed by a nasty visitor (I've also noticed that it refuses to work with AwardSpace.com).
If you'd rather be safe than sorry, use this:
$rootpath = "/home/USERNAME/public_html/";
Instead of this:
$rootpath = trim(strip_tags($_SERVER['DOCUMENT_ROOT']));
You'll need to know your absolute path, though.
@include("{$rootpath}/header.php");
"include()" is a function. This basically pulls one file into another.
The "@" sign before the include function will just hide any errors that may come up if the file you're trying to include doesn't actually exist.
The "{$rootpath}" part of this code is, predictably, the server path to your root directory.
"/header.php" is the path from the root directory to where your header.php sits. If you had "header.php" in a folder called "includes", "/header.php" in this code would actually be "/includes/header.php".
The "footer.php" snippet at the bottom of your page does the same thing, only with the footer file instead of the header file. :)




