Jump to content

TUTORIAL: Save time using the PHP include Function <?php include

- - - - -

  • Please log in to reply
13 replies to this topic

#1
Dan

Dan

    Programmer

  • Members
  • PipPipPipPip
  • 145 posts
One of the main problems faced by webmasters is saving time when it comes to creating websites. When starting a website, one may choose to create a navigation menu like the following to link to each page:

<a href="http://www.yoursite.com/index.htm">Home</a> - 

<a href="http://www.yoursite.com/about.htm">About Us</a> - 

<a href="http://www.yoursite.com/links.htm">Links</a> - 

<a href="http://www.yoursite.com/contact.htm">Contact Us</a>

Which looks like: HomeAbout UsLinksContact Us

So now, you have your 4 pages - and all is well. But, as the site expands you need to integrate a forum - which leaves you with 4 pages to update. No problem at all... But supposing the site had 100+ pages! That would be a very monotonous time consuming job.

This is where we bring in the php include command to really speed things up!

Create just the navigation menu on its own, and rename all of your pages to .php

In this case, the navigation menu will look like this:

<a href="http://www.yoursite.com/index.php">Home</a> - 

<a href="http://www.yoursite.com/about.php">About Us</a> - 

<a href="http://www.yoursite.com/links.php">Links</a> - 

<a href="http://www.yoursite.com/contact.php">Contact Us</a>

Which looks like: HomeAbout UsLinksContact Us

Save this file as "menu.inc" as we will be including it elsewhere - hence the .inc extension.

Now, open up your .php pages that the menu links to, and simply add the following code at the very top, to include the navigational menu on the pages:

<?php include("menu.inc"); ?>

The code is telling the php page, to "include" the menu.inc file - and it does!

Once this has been added, any changes in the menu.inc file are reflected across the whole site making updating a lot easier!

Why stop at menus? This can be utilised for adverts, footers, headers, templates and much much more!

#2
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
I includes like this to do most of my PHP work. I don't see a point typing the same info over again and if you want to change it you would have to change it on each page. With include you change once.

#3
Dan

Dan

    Programmer

  • Members
  • PipPipPipPip
  • 145 posts

Lop said:

I includes like this to do most of my PHP work. I don't see a point typing the same info over again and if you want to change it you would have to change it on each page. With include you change once.

Exactly the reason why I started using PHP! Of course once I discovered the powerful stuff it could do I never look back, I now build all websites in PHP.

Hope some strictly HTML users find this tutorial useful!

#4
DevilsCharm

DevilsCharm

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 884 posts
This is some PHP that I do know, I use the php include function on all of my pages. I have a global header and global footer, so I have to use it at least twice on all pages (but on some pages I have a third one).

#5
Guest_Kaabi_*

Guest_Kaabi_*
  • Guests
PHP has a lot of great functions, but I haven't heard of a PHP include one before. But then again, I know very little about PHP.

#6
xtraze

xtraze

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 910 posts
I find this really helpful, does this obey the CSS or just show as default ?
can I put it inside a <div> ?

#7
ervan

ervan

    Newbie

  • Members
  • Pip
  • 1 posts
all php working?

#8
Jerryn

Jerryn

    Newbie

  • Members
  • PipPip
  • 16 posts
I have recently started learning PHP again (stopped due to busy schedule) and from what I've understood, using .inc extension might not be the wisest thing to do. Using it for simple HTML files is ok, but if beginners forget that .inc's source code can be read by anyone, then they might include files that store very valuable PHP information. (password for database for instance)

So my suggestion to beginners is to create a new folder in your main path called "includes" and keep every file you include there. then just link it properly. (e.g. <?php include("includes/config.php"); ?>)

But since you're talking about creating the website layout, then you can also use .html extension. e.g. header.html, footer.html, menu.html etc.

#9
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Yes, it is often standard practise to add the .inc filetype to be forced through the PHP interpreter, however people often forget to do this or do not realize to do this after moving the script.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#10
Calgon

Calgon

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
It's also wise to consider smarty for something like this, it's great for publicly released content too.

#11
t0asty

t0asty

    Newbie

  • Members
  • Pip
  • 7 posts

xtraze said:

I find this really helpful, does this obey the CSS or just show as default ?
can I put it inside a <div> ?

Just apply the links to a div in the menu.inc file. So it would look like this:


<div id="style">

<a href="http://www.yoursite.com/index.htm">Home</a> - 

<a href="http://www.yoursite.com/about.htm">About Us</a> - 

<a href="http://www.yoursite.com/links.htm">Links</a> - 

<a href="http://www.yoursite.com/contact.htm">Contact Us</a>

</div>

Because everytime you include the menu.inc file it includes everything inside that file. So if you apply the div tags in there then they will also be included. You will just have to link to the css file in the page your including it on. So your index page for example.

Edited by t0asty, 12 January 2012 - 03:24 PM.


#12
Jeremy Morgan

Jeremy Morgan

    Newbie

  • Members
  • Pip
  • 6 posts
  • Location:Hillsboro, Oregon
Remember to be careful using .inc files, for security reasons. They can be parsed as all text and if there is any sensitive code in there, it can be exposed with Google hacking.

For instance, never put any login info into an .inc file! A simple google search will get you tons of logins for databases all over the net.

Other than that, yes i agree using include can be very handy. Smarty also works really well but some people don't need the functionality and it becomes a bit of a waste. You can do an include of a header and footer and be on your way.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users