CodeLair scripts and tutorials; click to return to home page

PHP Include

Utilise PHP includes on your website to make changes in design and navigation much easier. Also features a cool way to make your own 'image viewer'.
Last updated: April 30, 2007

The 'include' function in PHP is probably one of the most useful of all of them. Using this, you can import any document from anywhere into your web page dynamically. If you have dozens of pages with the same navigation, don't you find it annoying when you want to add a page to the link list? With PHP include you can write one page with the navigation and import it into every page on your site. And when you change this navigation page, the navigation on every page changes too!

Another use of this is to create one page with your design, then use GET parameters in the URL to display the required content.

 

Basic PHP include

Create a PHP file in your text editor (open a new document and save it with a .php extension). Add in the code for your web page - anything not in <?php and ?> tags gets treated as HTML. Wherever you want to include another page, simply insert the following code:

<?php include("filename.php"); ?>

Replace filename.php with the name of the file you want to include. Upload both files to your site. When viewing your file you should see that the HTML in the file is displayed, and so is the code from the include file. Neat, huh?

 

Related functions

As a quick side note, there are some other similar functions in the PHP library:

require: This function will ALWAYS include the file specified, even if it's tucked away in an else clause that doesn't execute! Technically, this function should never be used, since include can be used with better-written code and control flow.
include_once: Only includes the specified file once in the page. If this function or the include function are called any more times on the same page, they will not be included.

 

Designing with include

If you frequently change your site's design (or even small parts of it), then PHP include will save you lots of time. There are two ways of implementing this, which I will demonstrate below.

1. Includes in every file

In this first method, you create all your pages as you would normally, but instead of having all the design elements in each file, you save them out to separate files and include them in each page.

First, make one page with your design on it. Then copy everything from after the <body> tag to where your content begins into another file, and save it as "top.php" or something similar. Then copy everything from where the content ends, to before the </body> tag into another file, called "bottom.php" or something similar.

Where the parts for the design were (before and after the page's content), add includes for the top.php and bottom.php files. Voila! They will be imported into the document. And whenever you change design or add something to the navigation it will change for every page which has the includes.

2. One file with "on-the-fly" includes

This method stores your design is one file, most commonly index.php, and includes pages based on a variable specified in the URL.

First make one page with your design on it, called index.php. This will serve as your home page. Next make a file called "home.html" or something similar (as long as it's not index.html or index.htm) and put what you want to display on the home page into this page.

For all the pages on your site, simply create '.html' pages with just the HTML/content in them and no design. Add the following code to index.php where the content should go:

<?php
  if($page) include("$page.html");
  else include("home.html");
?>

Replace home.html with whatever you called the file earlier. To view any of the pages on your site, you link to them with the URL:

http://www.yoursite.com/index.php?page=pagename

Change 'www.yoursite.com' to your URL, and 'pagename' to the name of the page you want to link to (do not include the .html on the end, this is done in the script).

That's it! In some ways, this second method is better than the first, since you don't need to set up lots of pages and it's easier to change. I prefer the first method usually, because it means you can use different page titles for each page: with the second method every page has the same title.

 

Making an image viewer

If you've been to a site with an image gallery, it's quite likely you've seen one of these. Many people like to have a design around their images and without PHP or another scripting language, this can mean writing hundreds of HTML pages. It also helps reduce plagiarism of your gallery, since you can't just go through the gallery, right-click and do "Save Target As..." on all your pictures. Here's how to make your own image viewer:

1. Create a page called images.php and put the code in for the design. Generally the design of an image viewer should be nice and simple, with relatively few graphics aside from the image you're viewing.

2. Where the image will be placed, put this code:

<img src="http://www.yoursite.com/images/<?php echo $image ?>">

Replace 'www.yoursite.com/images/' with the folder path to where the images are stored.

3. Link to the image viewer with the following link:

http://www.yoursite.com/images.php?image=imagefile.gif

As before, use your site's base URL. Change 'imagefile.gif' to the filename of the image you want to display. You must include the file extension in this script - this is so that you can link to ay image type (.gif, .jpg, .png etc).

4. Upload images.php to your site and test your image viewer.