#1 (permalink)  
Old 12-20-2005, 04:56 PM
Just starting out
 
Join Date: Nov 2005
Posts: 3
iTrader: (0)
Rep Power: 0
kentuckyslone is an unknown quantity at this point
Default PHP Includes Make a Webmaster's Life Easier

PHP Includes Make a Webmaster's Life Easier

Use php includes to make maintaining your site much quicker and easier. Basic php is very easy to implement and to use.

Create your pages using HTML code as you would normally do but name the page with a .php extension instead of the .html extension. In this example we will be working with the header but the process is the same for other uses such as a footer or a links list.

What is a php include?
A PHP include file is a method of placing part of the content of your page in an external location. An unlimited number of other pages within the website will be able to access the same file. This is an excellent way to maintain and manage redundant content such as headers, footers, and links. You place the content in an include and then you place a link to that file within your page HTML. Whenever you need to edit or update what is held in the include you will only have one file to work with to propagate the changes across every page that is coded to display the include file.

Changes to your header graphics; Additions or deletions to your links list; Easier control over advertising areas; Updating the copyright date in your footer; These are only some examples of how you can benefit by using php includes.

How to use a php include for your header
You can create a header.php so that most or all of your pages can have the same header. Whenever you need to change the header you just change one file and all of your pages will be updated.

For example if you will notice on the honestinformation.com website the top of the page is the same on almost every page you go to. This is the header. Regardless of which page you are on this header is being generated from the same php include. If I need to change or add a link to the links table I only need to change the one include file to update all of the pages. The alternative to this would be to edit each and every page - which would be very time consuming.

Step 1

Create a folder for the include files. For these examples we are calling the folder "include"

Step 2

Within this directory create a file named header.php. This file can contain your graphics and text for what you would like at the top of your page. When you create a php include do not put the usual header and body tags in it.

The header.php include should look something like this:

<center><img src="url for image"><h1>Title or other text</h1></center>

Note this is only a simpified example. You can put whatever you need into the include i.e. tables, graphics, links, etc.

Step 3

To call this header up on your page you will simply add a few lines of php code to the page. Place the php code on the page where you want it to appear. Such as with the header it should go right after your <body> tag.

<?php
include '/home/User or Account ID here/public_html/include/header.php';
?>

NOTE: The path for the include file will almost always be in this format. i.e. - '/home/Account ID/public_html/directory/name. If this does not work for you contact your webhost to ask what path you should use.

When you add this code to the page the header file will be displayed as though the code were on the page itself. This is known as SSI (Server Side Includes). The server will actualy interpret the include and send the information to the browser as though it were all on the same page. For this reason search engine spiders have no problem in navigating the page.

In this example I showed you how to create an include for your header. Includes can also be used for footers, tables, links lists, etc. Just about any content can be manipulated with php includes.

What should you use php includes for? Anything that is repeated throughout your site.

Imagine that you have a website that consists of 250 total pages. At the bottom of every page you may have a copyright notice and contact information or a row of links.

Now suppose a new year rolls around and you need to update the date in your copyright notice. Do you want to edit all 250 pages one at a time to accomplish this? With a php include you can change one file and the copyright date (or contact information or links...) will be updated on all of the pages!

There are many uses for php. But If you never use php for anything other than includes you will still benefit a great deal. I hope that you have learned something from this tutorial. Contact me if you have any comments or questions.


Copyright 2005 David Slone. You have permission to use this article on your website, e-zine, blog, or newsletter provided that you leave author information intact. You may get the HTML code for this article HERE

David
Reply With Quote
  #2 (permalink)  
Old 12-21-2005, 09:32 AM
Respect please im an active member
 
Join Date: May 2005
Posts: 169
iTrader: (0)
Rep Power: 20
nsvr is on a distinguished road
Send a message via MSN to nsvr
Default

Hmm, not completly correct, you can use include witouth the (), but the correct way is like:

Code:
<?php include("includes/header.php"); ?>
And i dont recommend to use the full path for includes, just do it like above. If you want to use the full path, use the variable $_SERVER["DOCUMENT_ROOT"], where the /home/username/public_html is already stored in like:

Code:
<?php include($_SERVER["DOCUMENT_ROOT"] . "/includes/header.php"); ?>

And remember if you do that, use superglobals, in this case the super global $_SERVER, although $DOCUMENT_ROOT will also work, but thats for older versions of php.

Another nice thing about php is that you can configure the auto_append_file and auto_prepend_file, with a htaccess file.
This way, php will automatically add at the beginning/end of the php file the header and footer configured by these.
Reply With Quote
  #3 (permalink)  
Old 12-22-2005, 12:02 PM
Getting good at this!
 
Join Date: Jun 2005
Posts: 86
iTrader: (0)
Rep Power: 11
shawon is an unknown quantity at this point
Default

Quote:
Originally Posted by nsvr
Hmm, not completly correct, you can use include witouth the (), but the correct way is like:

Code:
<?php include("includes/header.php"); ?>
And i dont recommend to use the full path for includes, just do it like above. If you want to use the full path, use the variable $_SERVER["DOCUMENT_ROOT"], where the /home/username/public_html is already stored in like:

Code:
<?php include($_SERVER["DOCUMENT_ROOT"] . "/includes/header.php"); ?>

And remember if you do that, use superglobals, in this case the super global $_SERVER, although $DOCUMENT_ROOT will also work, but thats for older versions of php.

Another nice thing about php is that you can configure the auto_append_file and auto_prepend_file, with a htaccess file.
This way, php will automatically add at the beginning/end of the php file the header and footer configured by these.
An Adding:

include() is approximately same like the require() but require actually mean, it have to be upto the every refresh on the page.
Reply With Quote
  #4 (permalink)  
Old 12-23-2005, 09:04 AM
Respect please im an active member
 
Join Date: May 2005
Posts: 169
iTrader: (0)
Rep Power: 20
nsvr is on a distinguished road
Send a message via MSN to nsvr
Default

Quote:
Originally Posted by shawon
An Adding:

include() is approximately same like the require() but require actually mean, it have to be upto the every refresh on the page.

An Adding:

include_once(); :p
Reply With Quote
  #5 (permalink)  
Old 05-15-2006, 09:41 PM
Just starting out
 
Join Date: May 2006
Posts: 20
iTrader: (0)
Rep Power: 0
wahpah is an unknown quantity at this point
Default

Wow. Thanks for the additions guys. Very helpful. Keep up the good work.
I'm not too savvy when it comes to coding, but I do respect people who have worked hard and know what they're doing. I started doing this a while back, but gave up when it came to go beyond html. Perhaps I'll go back and pick things back up.
Reply With Quote
  #6 (permalink)  
Old 05-24-2006, 03:20 PM
Just starting out
 
Join Date: May 2006
Posts: 14
iTrader: (0)
Rep Power: 0
e2thehu is an unknown quantity at this point
Default

Yes. PHP does make a webmaster's life easier. I use to do a lot of web designing. PHP is good.
Reply With Quote
  #7 (permalink)  
Old 05-26-2006, 11:00 AM
Just starting out
 
Join Date: May 2006
Posts: 11
iTrader: (0)
Rep Power: 0
Link is an unknown quantity at this point
Default

I just redid a 200+ page website I inherrited to use includes.
Previously, all the menu/nav was hardcoded in each file.
Not fun for updates.
Reply With Quote
  #8 (permalink)  
Old 10-23-2006, 01:19 AM
Not a newbie any more
 
Join Date: Oct 2006
Posts: 149
iTrader: (0)
Rep Power: 9
Hypnotique is an unknown quantity at this point
Send a message via ICQ to Hypnotique Send a message via AIM to Hypnotique Send a message via MSN to Hypnotique Send a message via Yahoo to Hypnotique
Default

PHP includes definitely help my life and am implementing it into one of my website that don't have it, so that way, in the future all of my websites have it. This has definitely saved me a lot of time on updates which is crucial since I am always having to run around to get something done.

Hypnotique
Reply With Quote
  #9 (permalink)  
Old 10-25-2006, 07:52 AM
Just starting out
 
Join Date: Oct 2006
Posts: 34
iTrader: (0)
Rep Power: 0
Gerry is an unknown quantity at this point
Default

Wow thia just keeps getting more confusing, I been using free sites past 7 years, they want to kep you a dummy so you will keep using them huh? This is great article, thanks!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -6. The time now is 07:47 AM.
Contact Us -
Powered by vBulletin® Version 3.5.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0