Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

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


  • Please log in to reply
13 replies to this topic

#1 Dan

Dan

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 140 posts

Posted 28 June 2006 - 06:08 PM

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

#2 Lop

Lop

    Speaks fluent binary

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1112 posts

Posted 02 July 2006 - 12:59 PM

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.
  • 0

#3 Dan

Dan

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 140 posts

Posted 02 July 2006 - 11:42 PM

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!
  • 0

#4 DevilsCharm

DevilsCharm

    Programming God

  • Just Joined
  • PipPipPipPipPipPip
  • 858 posts

Posted 05 July 2006 - 08:41 AM

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).
  • 0

#5 Guest_Kaabi_*

Guest_Kaabi_*
  • Guest

Posted 05 July 2006 - 06:28 PM

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.
  • 0

#6 xtraze

xtraze

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 872 posts

Posted 01 January 2007 - 08:19 AM

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

#7 ervan

ervan

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 21 November 2011 - 08:02 AM

all php working?
  • 0

#8 Jerryn

Jerryn

    CC Newcomer

  • Member
  • PipPip
  • 21 posts

Posted 01 January 2012 - 06:19 PM

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.
  • 0

#9 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 01 January 2012 - 08:25 PM

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.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#10 Calgon

Calgon

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 53 posts

Posted 04 January 2012 - 11:27 AM

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

#11 t0asty

t0asty

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 09 January 2012 - 02:43 PM

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.

  • 0

#12 Jeremy Morgan

Jeremy Morgan

    CC Lurker

  • New Member
  • Pip
  • 6 posts
  • Location:Hillsboro, Oregon

Posted 05 April 2012 - 09:06 PM

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.
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download