Web Design Forum: TUTORIAL: PHP Include Explained - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

TUTORIAL: PHP Include Explained How to use php include in basic way Rate Topic: -----

#1 User is offline   Sazzad 

  • Expert
  • PipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 628
  • Joined: 21-February 07
  • Reputation: 38
  • Gender:Male
  • Location:New York, New York
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 15 October 2007 - 01:41 PM

Title: Php Includes
Language: php, XHTML, CSS
Skill Level: Basic
Author: Sazzad Hossain & RIT webDEV club
Demo: http://webdev.netnub.com/template.php

Authors Note: PHP is perhaps one of the simplest language you can earn that can grant you soo much freedom with your site. In addition, it can also make your life a lot easier. If you are a webdesigner you will see why. First of all, php has many functions that allow you to substitute for a long list of codes. For example, in this tutorial I will talk about php include. What php include basically does is takes an entire page of coding from one location and brings it to the page where you requested it. So if I have a head section located in "/include/head.php" and I want it to show up in my index.php file; all I have to do is call for it using php include.

So whats an advantage of include?
If you want to modify your site, and you have tons of page, and do not want to modify each one of them separately; when you use php include, all you have to modify is the file your including. In this example, all you have to do is modify the header.php and footer.php (also the style.css) that will change the layout to whatever you wish. It sort of acts as an StyleChanger effect but it requires a bit more work.

Step 1: Create the file you want to call for. Example, this header file: "includes/header.php." So we have these codes for the header file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
	<title>

	</title>
	<link rel="stylesheet" type="text/css" href="includes/style.css" />
</head>
<body>
<div id="holder">
<!-- holds the title and the navagation bar -->
<div class="header">

	<!-- holds the logo -->
	<div id="logo">
		<img src="../images/logo.idontknow" alt="RIT Web Dev Logo" />
	
	</div>

	<div id="nav">
	<br />
		<a href="index.php" alt="home">Home</a>|
		<a href="forum.php" alt="forum">Forum</a>|
		<a href="members.php" alt="members">Members</a>|
		<a href="about.php" alt="about">About</a>|
		<a href="contact.php" alt="contact">Contact</a>
	</div>
	<hr />
	</div>

Step 2: Since we will not have the ability to edit this page for the pages that will have a different title, we will need to add a variable that will allow us to accomplish that. So in order for us to give custom title to our main pages, we will need to replace the title tags with the following:
<title>
<?php
	echo $title;
?>
</title>

Step 3: Now lets create our footer file (you can create a sidebar, but I'm skipping that for the sake of time). We don't have to make any changes in this file unless you want to include something else from another location. Lets call this file footer.php and put it also in the includes folder.
	<hr />
	<div class="copyright">
		© 2007 RIT Web Developers, All Rights Reserved <br />
			<a href="contact.php"> Contact Us </a>
	</div>
</div>
</body>
</html>

Step 4: Okay, since we are now done with all the files we want to include in our index.php file, lets create the most important file: index.php. A general HTML file without header and footer will look something like this:
<div id="content">
	<h1> Content Title </h1>
	<p>
	Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce quis ante.
	Ut sit amet arcu eu lacus sodales egestas. Sed metus risus, placerat vitae,
	molestie sit amet, consequat eu, erat. Quisque tincidunt, urna sed
	pellentesque adipiscing, risus nulla congue urna, et pharetra tortor massa
	in leo. Cum sociis natoque penatibus et magnis dis parturient montes,
	nascetur ridiculus mus. Quisque tellus. Phasellus iaculis lacinia diam. In
	nonummy, tortor id rutrum fermentum, metus lacus ultrices turpis, sit amet
	hendrerit pede leo vitae neque. Aenean lacinia metus eu tellus. Quisque
	tempor dolor at velit. Etiam luctus fermentum justo. Pellentesque erat
	mauris, consectetuer a, commodo at, ultrices vel, lacus. Proin sit amet
	leo. Vivamus sit amet tellus.
	</p>
</div>

But that does not include our header.php and footer.php. So we need to call for it. We can accomplish this by using 3 lines of php codes. So above all the codes in our index.php code, we want to include our header. We can do this by pasting the following code:
<?php
	include("includes/header.php");
?>

Step 5: Remember when we added echo $tittle; in our header to allow us to give the page custom title? Well this is the additional work needed to accomplish that. Before the area where it states include("includes/header.php"); we will add the following line of title:
	$title = "Page 1";

Simply replace the Page 1 with your own page title.
Step 6: Now its time for us to call our footer. This can be accomplished the same way as in step 4. Simple paste the following code towards the bottom of the index.php file.
<?php
	include("includes/footer.php");
?>


Well your done! What you have to note is that you can place footer.php and header.php in any folder you want, just make sure you call it correctly. Many php programmers make that mistake, thereby end up spending hours finding the problem, although the problem is simple. Just as a word of warning/caution Windows versions of PHP prior to PHP 4.3.0 do not support accessing remote files via this function.

If you put all these codes together, you should get a page similar to the demo page (unless I end up modifying the page). If you want to use the same css, then here it is:
.copyright {
}
.header {
	border: thick #000000;
	text-align:center;
}
#holder {
	margin: auto;
	width: 700px;
}
#logo {
}
#main {
}
#nav {
}
/***************
	Links
***************/
a:hover {
	text-decoration:underline;
}
a {
	text-decoration:none;
	color:#0066CC;
}


Cool Extras:
- PHP Date & Time function
0

#2 User is offline   Thomas Thomassen 

  • HTTP 503
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,254
  • Joined: 30-April 07
  • Reputation: 10
  • Gender:Male
  • Location:Trondheim, Norway
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 15 October 2007 - 04:25 PM

The worst use of include I've seen was someone that'd used include instead of functions. ... Yes, that's true. ... Yes, it's mad, I know, but someone had done it. I don't know how many hundred filed that project had. All small snippets that acted like functions...
0

#3 User is offline   Sazzad 

  • Expert
  • PipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 628
  • Joined: 21-February 07
  • Reputation: 38
  • Gender:Male
  • Location:New York, New York
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 15 October 2007 - 06:08 PM

LOL yes thats true, however, for someone like me who change the layouts of websites often, I would rather use includes. Using a style change function with php takes time (for me) and being a full time student who spends most of his days in the lab, its the best option for me.

Btw - I'm confused as to are you directing the worst case of include you've seen towards me or to someone else. (yes I know its a stupid question)
0

#4 User is offline   Thomas Thomassen 

  • HTTP 503
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,254
  • Joined: 30-April 07
  • Reputation: 10
  • Gender:Male
  • Location:Trondheim, Norway
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 15 October 2007 - 09:29 PM

No, not towards you. Not at all. It was solely referring to that piece of code I was talking about.
What you posted is good use of include. Sorry, I forgot to actually comment on your post. I'm a bit of an airhead and my brain tend to get ahead of itself sometimes which often results in what's being outputted skipping a few logical steps and chain of thoughts.
0

#5 User is offline   php_penguin 

  • richthegeek
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,471
  • Joined: 06-August 07
  • Reputation: 7
  • Gender:Male
  • Location:Liverpool
  • Experience:Web Guru
  • Area of Expertise:Coder

Posted 15 October 2007 - 09:51 PM

wow that is a truly bad method of developing (TT's examples, not Ian's). Just imagine the overhead...
0

#6 User is offline   Sazzad 

  • Expert
  • PipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 628
  • Joined: 21-February 07
  • Reputation: 38
  • Gender:Male
  • Location:New York, New York
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 15 October 2007 - 09:53 PM

Hehe I'm like that as well. Thanks for your comment. It's really a helpful source that I really love using. I'm actually re-designing my personal website now, and I am coding it in php. I realized that, so I thought why not share it people
0

#7 User is offline   Mark 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 164
  • Joined: 15-October 07
  • Reputation: 0
  • Location:Southampton, Hants, UK
  • Experience:Intermediate
  • Area of Expertise:Designer

Posted 24 October 2007 - 07:50 AM

Thanks for this. I am an extreme novice with PHP and as till now can could only use a basic PHP contact form.

This looks very useful to me, going to start implementing it into more of my sites.
0

#8 User is offline   Sazzad 

  • Expert
  • PipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 628
  • Joined: 21-February 07
  • Reputation: 38
  • Gender:Male
  • Location:New York, New York
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 09 November 2007 - 05:20 AM

I'm not sure why but the code where it states $title = "Page 1"; doesn't seem to work. I wonder why???

//EDIT
WOULD double == fix it?
0

#9 User is offline   CraigH 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 11
  • Joined: 04-October 07
  • Reputation: 0
  • Experience:Advanced
  • Area of Expertise:Web Designer

Posted 21 November 2007 - 11:24 AM

I've just tried the $title bit of code in my site and it's working fine for me, what kind of error are you getting from it?
0

#10 User is offline   Sazzad 

  • Expert
  • PipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 628
  • Joined: 21-February 07
  • Reputation: 38
  • Gender:Male
  • Location:New York, New York
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 04 December 2007 - 11:49 PM

Oh haha, how silly of me, I forgot the "$" sign before the title. Oops.
0

#11 User is offline   seige 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 104
  • Joined: 04-December 07
  • Reputation: 0
  • Location:Mid Wales/Shropshire Border
  • Experience:Web Guru
  • Area of Expertise:Entrepreneur

Posted 05 December 2007 - 07:48 AM

View PostThomas Thomassen, on Oct 15 2007, 16:25, said:

The worst use of include I've seen was someone that'd used include instead of functions. ... Yes, that's true. ... Yes, it's mad, I know, but someone had done it. I don't know how many hundred filed that project had. All small snippets that acted like functions...



This is a really nice article.

I agree with what Thomas says above... however, I'd also stress that for people learning PHP, showing them the INCLUDES methods and theory before getting dirty with functions is a great way to teach them how re-usable snippets work... and my reasoning behind this is that it is a very simple and 'visible' way of making the point. And of course, with *new* coders, you don't have to initially get into the complexities of variable scope when using includes, so again, another great reason why it's much easier to understand.

I've supervised the training of a couple of PHP coders now—guys who were mainly designers but needing to dabble back-end—and I've gone down the 'learn includes, THEN functions, THEN classes' route.

The great thing about this is that down the line, you can then teach that an include, and its constituents, can be written as functions for even MORE functionaily.

Last night I showed on this forum how a member could consolidate the coding they were using by utilising their include file better. The way I WOULD have shown this would be to use functions—but the guy had already said he was quite new to PHP.

So, a great thumbs up for this article—but remember, although there *IS* a right way, and there *IS* a wrong way, it's sometimes better to take tiny footsteps rather than a leap in order for people to understand.

Of course...

The other great thing about includes is that they can be conditional... and this is great.

For instance, I remember the not-so-distant past when HTML in it's most basic form was the only real way to code. Sites predominantly used FRAMESETS, and CSS wasn't even heard of.

Large site projects which had, for instance a common navigation or footer, would tuck them into a frameset.

For those of you unfamiliar with the now almost obsolete (thank goodness!) framesets, I will explain: a frame is a container, which can have a page loaded into it. So, imagine an index.html page which consisted of no content, just a description of 3 frames: the top frame (header & nav), a middle frame (body content) and a bottom frame (the footer).
Then, the index page would reference another file for each FRAME... i.e. the top frame could include 'header.html', the middle 'page1.html' and the footer 'foot.html'.

This was a great way of re-using page code... and if, for example, the footer info needed changing, you only need change it once and ALL your site pages would automatically reflect that change, as the footer file is dynamically loaded as a page component on each page in the site.

Now... includes can work in a very similar way. If you have, for instance, a similar layout model for the whole of your site, you could add a 'footer' include to every page to save you re-writing it. And a header.

So, why not just wrap your page in a header and footer include?

Think of it like this:

<html>
<body>
<? require_once('includes/header.php') ?>
<!-- body content for this page -->
<? require_once('includes/footer.php') ?>
</body>
</html>


Working this way is fine for code junkies like me, because I can visualise the page layout. But it's a little bit harder for GUI workers with WYSIWIG web editors, as the page content will not display in your editor.

Another good include...
...and one which I use on practically every dynamic site I do is the 'includes/setup.inc.php', which is called RIGHT AT THE TOP of the page, before any HTML or headers are output.

This include handles all the doctype HTML, the opening HTML tags and opens any DB connection, plus defines any common functions.
It also handles post data cleaning, page checking, and meta tags for description, title, stylesheets, java etc.

And, because it is used universally, it ensures your site is consistent.

A bit of a conlcusion about includes, and some other handy tips...
In short, includes shouldn't replace functions: they should CONTAIN them! When starting a dynamic site, the usual page construct for me is:

<?php 
require_once('includes/setup.inc.php';
<body>
<? require_once('includes/header.php') >
<!-- page content -->
<? require_once('includes/footer.php') >
</body>
<? require_once('includes/terminate.php') >


So... the tips here are: terminate.php closes anything, including DB and tags created by the setup.inc.php include, such as the 'HTML' opening tag.
The terminate file can also include error tracking scripts, etc.

Now, another tip... REMEMBER that when you include a file, it's root paths and variables are all relative to the parent file, even though the includes may be in a separate directory, or even server. This has been the simple solution to MANY problems I've had over the years!

For instance, imagine this construct in a file called index.php:

include('pictures/gallery/imageshow.php');


Now... the picture this file calls is within the 'pictures' folder, which resides at the root level, the same as the index.php file calling the include.

Providing a relative path to that picture in imageshow.php include will NOT work, as using a RELATIVE path will be relative to the PARENT PAGE and not the INCLUDE. Because the include is buried deeper, a relative path within the include file would have to escape the 'gallery' folder first!!!

It's very good practice to use absolute URLs in sites anyway, and in this instance, it would sort out the problem.

But more about that another time, cos I need coffee!
0

#12 User is offline   Thomas Thomassen 

  • HTTP 503
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,254
  • Joined: 30-April 07
  • Reputation: 10
  • Gender:Male
  • Location:Trondheim, Norway
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 05 December 2007 - 05:31 PM

Framesets... *shivers*

/me curls up in a corner until the flashbacks retreat.
0

#13 User is offline   declaration_end 

  • Forum Newcomer
  • Pip
  • View gallery
  • Group: Members
  • Posts: 64
  • Joined: 10-June 07
  • Reputation: 0
  • Location:WALES
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 13 December 2007 - 09:56 PM

I've been meaning to get around to learning this - I'm glad I stumbled in here 'cause I have a basic static site that is starting to get out of hand with updating many similar sections. Apart from tying to theme wordpress also, in the mean time I'm sure I can implement this.

Top tutorial! And thanks for posting it, because even the simple things can help.
0

#14 User is offline   kip0130 

  • Expert
  • PipPipPipPip
  • Group: Members
  • Posts: 743
  • Joined: 24-June 07
  • Reputation: 0
  • Experience:Beginner
  • Area of Expertise:Coder

Posted 17 December 2007 - 12:43 PM

thanks for the great link :) im getting my grasp on basic PHP i dont feel a urgent need to be A hardcore php coder.
0

#15 User is offline   Sazzad 

  • Expert
  • PipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 628
  • Joined: 21-February 07
  • Reputation: 38
  • Gender:Male
  • Location:New York, New York
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 18 December 2007 - 03:39 AM

Thank you very much for the comment. I noticed how on many tutorial sites, they expect the users to have some sort of knowledge on the language. I found that not helpful. When a friend of mine needed help maintaining his site, I wrote this for him. Laster I decided to share it with others. I am currently working on another tutorial related to the basics of php. I hope all php coders will help me make that thread a great big support for beginners.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users