Include Layouts
A neat, simple and safe way of PHP-skinning your site.
Code & Install
<?php
if( !empty( $_SERVER["QUERY_STRING"] ) ){
if( @!include( "content/". $_SERVER["QUERY_STRING"] .".inc" ) )
show404();
} else {
if( @!include( "content/home.inc" ) )
show404();
}
function show404(){
if( @!include( "content/404.inc" ) )
print "No such file.";
}
?>
To 'install' this script, first create a new, empty text file; this
will be your 'template' file. Usually it will be your index.php file,
though not necessarily always. Technically you can call it whatever
you want, so long as it has the .php extension. For the sake of the
rest of this tutorial, we're going to call it index.php.
Into this file you should put your site's template layout; usually just plain HTML. This is all the stuff that will be the same no matter what page of your website a visitor goes to. Insert above script into this file wherever you want the changing content to appear, such as in the example below:
<html>
<body>
This is a very simple website.
<?php
if( !empty( $_SERVER["QUERY_STRING"] ) ){
if( @!include( "content/". $_SERVER["QUERY_STRING"] .".inc" ) )
show404();
} else {
if( @!include( "content/home.inc" ) )
show404();
}
function show404(){
if( @!include( "content/404.inc" ) )
print "No such file.";
}
?>
</body>
</html>
In the above, the text 'This is a very simple website' will appear on every page.
Next you need to create the 'content' files. These should all be
located in a folder called content which should be
located in the same directory as your index.php file.
There are only two files that the script 'needs'; home.inc
and 404.inc. home.inc is what will be displayed
whenever a user visits your index.php page directly,
with no query string (everything after the '?'). 404.inc
will be displayed if the script cannot find the requested page.
(As an absolute failsafe, if 404.inc is not present,
the script will display the message No such file. as a
default.)
Other than these two files, you can fill you content/ folder
with whatever you like. The only caveat being that include files
for the script must end in .inc. This is for security reasons;
you could have content/image.gif but you wouldn't be able
to include it directly by this script.
Files in the content/ folder are included based on the
URL (technically the querey string). So, for example, if a user
visited http://mydomain.com/?about (or
http://mydomain.com/index.php?about, either is fine) the
script would include content/about.inc.
The include files themselves should be just regular HTML; you can
even include other PHP scripts this way! The only difference is
that you don't need to include the main 'layout' in each of these
files, since that is all stored in index.php. That way,
when you change your layout, you only need to change one file, as
opposed to half a dozen or more; easy!
Known Issues
Because of the 'supershort' way this script reads query strings, you won't be able to use this script in conjuction with any other that also wants to put any other data into the query string. The most notable example would be things that use sessions, like sk.ring. To get around this, use the following code:
<html>
<body>
This is a very simple website.
<?php
if( !empty( $_REQUEST['x'] ) ){
if( @!include( "content/". $_REQUEST['x'] .".inc" ) )
show404();
} else {
if( @!include( "content/home.inc" ) )
show404();
}
function show404(){
if( @!include( "content/404.inc" ) )
print "No such file.";
}
?>
</body>
</html>
Instead of http://mydomain.com/?section you will need to
use http://mydomain.com/?x=section.
Alternately, you may simply prevent PHP from writing session tokens to the query string with the following line:
ini_set('session.use_trans_sid','0');
This will have the side effect of potentially breaking your script for anyone who disables or blocks cookies, so use it sparingly.