Example Server Side Includes

Using a server side include (SSI) in your pages can allow you to simplify writing and maintaining them. You could use a SSI to simply include in one line a standard bit of HTML that you put in all your pages or you could use a more complicated SSI to include dynamic content depending on the page it is included in. For example you could set the background image or colour of your pages with a SSI. Then if you want to change the background you only need to change the SSI to affect all pages. Another example would be the footer you put on every page saying who maintains it and when it was last updated. This SSI could automatically work out the date the page was last updated so you need never again remember to change the date at the end of a page. Also if you want to change the format of the footer you only have to change the SSI and all pages will take on the new footer style.

Example SSI's

NOTE: for a server side include to work the page that includes it must either have a filename of the form *.shtml or have an execute bit set, e.g. do chmod 744 file.html to make it mode 744 rather than the more normal mode 644 (It is recommended that you use the *.shtml method, the execute bit hack is only for backwards compatability for some older pages and will be switched off in the future).

Setting the background

In the HTML document that you want to set the background you could put the HTML code


<body <!--#include virtual="path_to_body-tags.ssi" --> >

in place of your opening body tag. This says the body tag parameters are to be included from the file path_to_body-tags.ssi. So if you write a file called body-tags.ssi and place it in a subdirectory includes of your public_html directory then the path_to_body-tags.ssi is /~username/includes/body-tags.ssi. The body-tags.ssi file could be something like


background="/~username/images/paper.gif"

      text="#000055"  link="#FF0000"

      vlink="#0000AA" alink="#00FF00"

to define a background image and link styles or it could be more simple like


bgcolor="#99CCFF" text="#000000"

to define a background colour and text colour.

Defining the Author Meta Data

In the HTML document that you want to set the Author meta data you could put the HTML code


<!--#include virtual="path_to_head-tags.ssi" -->

somewhere between the opening and closing head tags. If you write a file called head-tags.ssi and place it in a subdirectory includes of your public_html directory then the path_to_head-tags.ssi is /~username/includes/head-tags.ssi. The head-tags.ssi file could be something like


<meta name="Author" content="My Name">

<link rev="made" href="mailto:me@maths.ox.ac.uk">

where obviously you should replace `My Name' with your own name and `@email' with your email address. Adding the meta data as a server side include makes it easy to change the name of the maintainer should the ownership of the pages change (e.g. with research group pages).

Automatic Footer

In the HTML document that requires the footer setup any variables it will use and then include the SSI


<!--#set var="updated_by" value="your_name" -->

<!--#set var="maintainer_email" value="your_email_address"-->

<!--#include virtual="path_to_footer.ssi" -->

You would normally include these 3 lines just before the end of your HTML file (say before the closing body and html tags).

Then it can call upon the SSI footer.ssi to format this info and put in the last modified date automatically. The SSI file footer.ssi could be


<hr>

<p align=right><small>

This page last modified

<!--#if expr="$updated_by" -->

  by <!--#echo var="updated_by" -->

<!--#endif -->

<br><!--#echo var="LAST_MODIFIED" -->

<!--#if expr="$maintainer_email" -->

<br>Email corrections and comments to

<em><a href="mailto:<!--#echo var="maintainer_email" -->"><!--#echo var="maintainer_email" --></a></em>

<!--#endif -->

</small></p>

If you put the footer.ssi file in your public_html directory in a subdirectory called includes then the path_to_footer.ssi required above would be /~username/includes/footer.ssi.

Please contact us with feedback and comments about this page. Last updated on 02 Apr 2022 21:54.