Science Library - free educational site

Dynamic Site Structure with PHP

The early days of the Internet were hampered by low bandwidth and restricting programming capabilities. As time went on, sites got more sophisticated, attempting to meet the requirements of an ever broader set of applications and user expectations.

PHP is perhaps the world's most used server-side scripting language. It is a very flexible programme, which allows for open-architectural solutions to sites. This basically means it has the means to create modular elements which can be included on any page, while leaving open options for the evolution of the site through time.

Including external files

A very useful technique to create large, complex sites, which retain consistency in common page elements, is the external file include.

For example, to avoid problems when a header or footer is modified, a large site will call a single header onto every page that is loaded. This header or footer file can be edited at will, and will automatically appear on any page on the domain that calls them. Fast, reliable and consistent.



require(BASE_URL .'inc/');


These are all examples of code which will include the nominated files provided the code is placed somewhere on an HTML file within PHP tags (<?php ..... ?>).

The extension *_once avoids having the programme call the same file more than once. This can happen in complex code, and, although it theoretically should not be a problem, in practice it is best avoided. In any case, streamlining download times requires good practice in avoiding repetitive and unnecessary tasks.

The difference between include and require is in the response from the script when the call to include the file fails. When include fails to load the file, the programme prints a warning, but otherwise shrugs its digital shoulders and soldiers on. A require failure, on the other hand, assumes the file is essential (such as for connecting to a database) for the good function of the following script, so prints an error and calls the whole script out on strike.

The .inc in the file name is not technically necessary. include('../inc/footer.php') would work just as well. However, experience indicates that putting the .inc postscript in the file name is good housekeeping practice, and allows for ready identification of common included files. For similar reasons, included and required files are best placed in a dedicated common folder in the root of the site. These can then be called using the 'BASE_URL' path defined as a constant in the file. Note that since 'BASE_URL' is defined in the file, it cannot be used in the pathname when including the file!

HTTP Headers

HTTP stands for 'Hypertext Transfer Protocol', and is a key factor in how the World Wide Web operates. It defines how clients and servers communicate, when a page is requested by a browser.

A request to a server for a Web page will be replied with a series of HTTP headers. PHP uses the header() function to make dynamic use of this protocol. A typical application is the redirection of a page from one location to another. But they may also be used to redirect pages, send files, set cookies, and control page caching.

The basic syntax is header(header string);. For example header('Location:'); redirects the page to the root of

Sending a file by HTTP


header("Content-Disposition:attachment; filename=\"vitruvian-boy-audiobook.mp3\"\n");

Content-Type specifies the MIME (Multipurpose Internet Mail Extensions) type of the file. Possible MIME types are: 'image/jpg', 'image/png', 'application/pdf', 'audio/mp3', 'video/mp4'.

Content-Disposition instructs the browser concerning its next step: attachment will prompt the browser to download the file. inline will prompt the browser to display the file.

headers already sent is a common error which is triggered when something has been sent to the browser before the header() function is called. Nothing, not even a space, must be sent to the server ahead of the header() call.

Content © Renewable.Media. All rights reserved. Created : August 24, 2014 Last updated :October 8, 2015

Latest Item on Science Library:

The most recent article is:

Air Resistance and Terminal Velocity

View this item in the topic:


and many more articles in the subject:

Subject of the Week


Mathematics is the most important tool of science. The quest to understand the world and the universe using mathematics is as old as civilisation, and has led to the science and technology of today. Learn about the techniques and history of mathematics on


Great Scientists

Dmitri Mendeleev

1834 - 1907

Dmitri Mendeleev, 1834 - 1907, was a Russian chemist who developed the modern Periodic Table of Elements.

Dmitri Mendeleev, 1834 - 1907, Russian chemist