How does PHP Include
work?
My example below illustrates how to make the top and
bottom
navigation elements of your web page dynamic. This will
enable you to update 1000s of pages by just editing two
PHP
Include files.
1. Make sure your web host has PHP installed. Most
reputable hosts do but check with them anyway.
2. Place the content between each of the comment tags
below
(ie <!--HEADER --><!--HEADER -->) in a text file and name
it with a .php extension ie header.php (or header.txt) You
can use notepad for this.
Here is an example of a typical web page using CSS
(cascading style sheets) to style the header content ie
<div id="header"></div>.
<html>
<head>
<title> This is the page title </title>
<head>
<body>
<!--HEADER -->
<div id="header">
<h1 > This is the header</h1>
</div>
<!--HEADER -->
<!--TOPNAV -->
<div id="topnav">
<ul>
<li><a href="index.php">HOME</a></li>
<li><a href="aboutus.php">ABOUT US</a>
</li> <li><a href="contactus.php">CONTACT US</a></li>
</ul>
</div>
<!--TOPTNAV -->
<!--LEFTNAV -->
<div id="topnav">
<ul>
<li><a href="index.php">HOME</a></li>
<li><a href="aboutus.php">ABOUT US</a></li>
<li><a href="contactus.php">CONTACT US</a></li>
</ul>
</div>
<!--LEFTNAV -->
3. Name the other include files ie topnav.php and
leftnav.php
4. Create a folder called includes and place the
header.php, topnav.php, leftnav.php files in it.
5. Replace the text between the comment tags with
your PHP
includes code. i.e.
Replace:
<div id="header"> <h1 > This is the header</h1></div>
With:
<?php include 'includes/header.php'?>
Do the same for the topnav and leftnav sections.
7. Save your new web page with a .php extension ie
index.php (If you save it with a different extension your
page will get errors when you upload it to your server.
8. Upload your includes folder and CSS style sheet
(if you
created one) to the root folder of your server...usually
it's the public_html folder.
9. Open the new web page in your browser. The
header,
top navigation and left navigation sections should appear.
Experiment with changing your include files or making
other
areas of your web page dynamic. You can then do the same
for future web sites you build and save lots of time
maintaining
them.
Frequently asked questions:
Question 1
How can I use PHP include for easy site
maintenance if all my web pages currently have .html or
.htm extensions? I don't want to lose their current search
engine rankings.
Answer
Insert the code below into notepad, name it
.htaccess and upload it to the root folder of your server.
AddType application/x-httpd-php .html
AddType application/x-httpd-php .htm
Now just follow the 9 steps as outlined above and you will
be able to keep your current page extensions ( .htm..html
or .php) and still use PHP include. This will allow you to
keep the same search engine rankings as you had before you
made your site dynamic.
Question 2
How can I include content from other sites
using PHP include (not possible using SSI)?
Answer
Insert this code on your web pages:
<? include('http://www.someothersite.com/directory/file.php');?>
Question 3
What PHP include code do I use for web pages
if they are positioned one or several directories away
from the
root files of my server? I keep getting errors when I
upload them.
Answer
The best way is to use the full path from the site URL. ie
<?php include 'http://www.domainname.com/directory/includes/topnav.php'?>
=====================================================
Herman Drost is a Certified Internet Webmaster (CIW)
and owner of iSiteBuild.com
Web Site Design, Web
Hosting, Web Site Promotion
Author of “Marketing Tips” newsletter. To subscribe, enter
your information at:
http://www.isitebuild.com/articles |