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.

include('../inc/header.inc.php');

include_once('../inc/mysqli_connect.inc.php');

require(BASE_URL .'inc/footer.inc.php');

require_once('../inc/config.inc.php');

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 config.inc file. Note that since 'BASE_URL' is defined in the config.inc file, it cannot be used in the pathname when including the config.inc 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: http://sciencelibrary.info'); redirects the page to the root of ScienceLibrary.info.

Sending a file by HTTP

header("Content-Type:audio/mp3\n");

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.com. All rights reserved. Created : August 24, 2014 Last updated :October 8, 2015

Latest Item on Science Library:

The most recent article is:

Trigonometry

View this item in the topic:

Vectors and Trigonometry

and many more articles in the subject:

Subject of the Week

Resources

Science resources on ScienceLibrary.info. Games, puzzles, enigmas, internet resources, science fiction and fact, the weird and the wonderful things about the natural world. Have fun while learning Science with ScienceLibrary.info.

Science

Great Scientists

Richard Owen

1804 - 1892

Richard Owen was a prominent figure in British paleontology at the time of Darwin. Apart from his controversial stance against the theory of evolution as presented by Darwin, which lost him scientific credibility, he was a driving force in the creation of the 'museum for the people' concept.

Richard Owen, 1804 - 1892
IT Forum by Sean Bone

Quote of the day...

The Facebook filter bubble is not a technical but an anthropological problem.

ZumGuy Internet Promotions

English language school