October 15, 200718 yr 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
October 15, 200718 yr 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...
October 15, 200718 yr Author 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)
October 15, 200718 yr 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.
October 15, 200718 yr wow that is a truly bad method of developing (TT's examples, not Ian's). Just imagine the overhead...
October 15, 200718 yr Author 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
October 24, 200718 yr 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.
November 9, 200718 yr Author 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?
November 21, 200718 yr 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?
December 5, 200718 yr 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!
December 13, 200718 yr 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.
December 17, 200718 yr thanks for the great link im getting my grasp on basic PHP i dont feel a urgent need to be A hardcore php coder.
December 18, 200718 yr Author 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.
Create an account or sign in to comment