|
How To Use "PHP Includes"
Many less experienced webmasters
(and webmistresses) are very nervous about using "techie"
stuff such as the web programming language, "PHP".
Relax! You don't need to learn such programming from
scratch! You just need to find code snippets which
other people have created - in most cases it's quite
legal.
One such example is using "PHP
Includes", which can save you a huge amount of time
when you're building websites which keep on growing,
or when you want to change the same web page element
on many different pages.
Take the "My Tricks" index on the
left of this page. Each time I add a new "trick" the
link needs to be added to the index - and the index
appears on every page of the site.
Do I need to change every page?
If I did, the job would take longer and longer each
time I added a new page.
But, by using a PHP include snippet,
I need to change and upload just one file - and the
changed content appears on every page of this site.
Here's how you can do it easily
(but please read the "Important Note" lower down this
page):
Use your normal web authoring software to create a web
page with all the content that you want to include
on each page of your site. In my example, I just created
a page with a table and links to each of my "tricks"
pages.
Go into the "source" or "code" view of the page you've
created and strip out all the normal HTML page tags
(marked in red below)
<html>
<head>
head content
</head>
<body>
This is the content you created for including on every
page
</body>
</html>
Save the page with a ".php" extension not an
".html" extension e.g. "menu.php".
You can save it wherever you like - I usually save such
files in a folder on my server called "includes".
But for the sake of simplicity, I'll assume in this example
that the "menu.php" file
is saved in the same folder as the web pages it's going
to be inserted into. I'll refer to this below as the
"includes file".
Now open up each page on which you want this content
to appear and, in the "source" or "code" view of
your web authoring software, go to the place where
you want the content to appear and type in this PHP
code snippet:
<?php
include("menu.php")
?>
Change the bit in red to the file
name of your includes file. If you have put
this file in a folder below the folder where
your regular web pages are located, you'll need to
put the link to the file something like this:
<?php
include("my-includes/menu.php")
?>
.. where the bit in red is
the name of the folder where your includes files are
stored - "my-includes" in the example above.
If you have trouble in the "code"
view of seeing where exactly to put the code snippet,
here's a tip that I use.. in the "normal" or "design"
view in your web authoring software, put some marker
text, such as "here". Highlight this text and switch
to "code" view. Most web authoring software will show
this marker text highlighted in the "code" view. Then
you can simple delete this marker text and insert the
code snippet.
And that's it! Once you've gone
through the task of putting the PHP include code snippet
on every page and you've uploaded those pages - plus,
of course the includes file, you need never touch those
pages again.
Whenever you want to change the
content - as I'll be doing when I've finished writing
up this "trick" - just open up your includes file,
make the changes and upload the file to your server.
All your web pages will then show the changed content.
Important Note:
Every web page which runs PHP code - e.g. the
pages where you have inserted your PHP Include code -
should have the extension ".php", not ".html" or ".htm".
If you are creating new pages with the code snippet,
that's not a problem.
But you don't want to start renaming
all your existing pages, particularly if they are indexed
by the search engines.
If this is the case, you can get
round this problem by using a ".htaccess" file. You
can see how to do this on this page:
http://php.about.com/od/advancedphp/p/html_php.htm
It might seem a little complicated,
but it's not really.
What
a
great
time
saver!
I
hope
you've
found
this
useful.
Regards
Ian Traynor
Professional Webmaster
P.S. If you want
to thank me for this "trick", here's
a PayPal donations button. Click on it to pay me anything
you like, using either your PayPal account or your
credit card:
|