PHP Include
In PHP the include is a function not a directive. This makes it useful for far more than importing data and pages. The article here however is going to focus on just the basic file include useage. Two methods of including files and code modules are available with PHP.
<? include("filename") ?>
and
<? require("filename") ?>
From PHP version 4.0.1pl2 there are two more related functions available with "include_once()" & "require_once()" but these are beyond the scope of this article. You can read more on all these functions in the PHP Manual PHP Control Structures section.
The Include Function
The useage and effects of "include()" & "require()" are identical, the only difference being in the error handling
Quote from the PHP manual
require() and include() are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, don't hesitate to use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well.
root---- www
|----include
| |
| |---functions.php
| |---footer.php
| |---header.php
| |---style.css
|
---reports
|---index.php
|---content.php
To have default.asp include functions.php, header.php, content.php and footer.php the page code would be;
<?php include("../include/functions.php");?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Reports Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="Reports page">
<meta name="keywords" content="keywords">
<link href="/include/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php include( "../include/header.php");?>
<?php breadcrumbs();?>
<?php include("content.php");?>
<?php include("../include/footer.php");?>
</body>
</html>
One issue that can cause problems for anyone new to coding includes in PHP is the lack of a substitute for the virtual argument. The PHP include function does not handle a leading slash ("/"). This means that root relative paths
<? include("/include/filename") ?>
to allow the same code line to serve for the whole site structure cannot be used. A very important factor if you use a layout file as a template with all the common features built in and then just change the content section.
To work around this and allow the use of a single include folder that is accessible from the whole site structure, the include function call will need to be hardcoded with an absolute path to the included file.
<? include("http://www.sitename.com/include/filename") ?>
This then raises another possible issue where a developer has a standard code library that is copied from site to site and in this case every instance of the include functions will need editing to reflect the new site name. A task where it would be very easy to miss a line which could cause some odd problems when testing.
So scripting has to come to the rescue here by using a value retrieved from the server. A simple line of PHP code added to the top of each page (or in the template) allows for the hostname to be set in a variable and concantenated to the filename.
<?
$domain = 'http://' . $_SERVER['HTTP_HOST'];
?>
<? include($domain . "/include/filename") ?>
The page code will then look like this;
$domain = 'http://' . $_SERVER['HTTP_HOST'];
?>
<? include($domain . "/include/filename") ?>
<?
$domain = 'http://' . $_SERVER['HTTP_HOST'];
?>
<? include($domain . "/include/functions.php");?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Reports Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="Reports page">
<meta name="keywords" content="keywords">
<link href="/include/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<? include( $domain . "/include/header.php");?>
<? breadcrumbs();?>
<? include("content.php");?>
<? include($domain . "/include/footer.php");?>
</body>
</html>

