Phone: 406-788-9165

Server Side Includes (SSI)

Article Index

  1. Apache Handlers
  2. The "include" Directive
  3. Time Formats
  4. Condtional SSI
  5. Time Limited Content using Conditional SSI
  6. Page Changed and Copyright Date Footer
  7. File Sizes

Apache Handlers

Do you want to use Server Side Includes on your standard .html documents?

If you have root access and can change your apache config files, you can add the following to your httpd.conf or vhost.conf. This will allow Apache to treat .html the same as .shtml!

Example:

1.
2.
3.
Options +Includes
AddType text/html .html
AddOutputFilter INCLUDES .html

For those who don't have root access but have access to cPanel. you simply need to follow these steps:

  1. Log into your cPanel
  2. Locate and click on "Apache Handlers"
  3. For the handler, you will input "server-parsed"
  4. For the extension, you will input ".html"
  5. Click the Add button to save

If your host gives you the option to use a .htaccess file, add the following line to your .htaccess file. If this file does not exist, you can simply create it in notepad.

Example:

1. AddHandler server-parsed .html .htm

The "include" Directive

The very basic use of SSI (and perhaps the most useful) is that of the "include" directive. You can use it to display common elements of a website. Common elements can be things like navigation menus, headers and footers.

<!--#include file="file"--> # path relative to the directory of the parsed document.
<!--#include virtual="/path/to/file"--> # virtual path to the directory of the parsed document.

The main difference between "file" and "virtual" is with file you cannot use "/" or "../" etc...

Example:

1.
2.
3.
4.
<!--#include file="monkey.txt"-->
Monkey's are cute. Monkey's have opposable thumbs.
<!--#include virtual="/path/to/llama.txt"-->
Llamas smell strange. Yes they do!

Time formats

Using Time formats is easy. we use a config directive.

<!--#echo var="DATE_LOCAL"--> # Echo current date and time.
<!--#config timefmt="value"-->

This will configure the SSI time formats used to echo dates and times or in conditional processing. There are many different values to enter to get the info you want to show:

%aabbreviated weekday name %Afull weekday name
%babbreviated month name %Bfull month name
%cprefered date and time format %dday of month - 01 to 31
%Ddate as %m/%d/%y %eday of month - 1 to 31
%Hhour - 00 to 23 %Ihour - 01 to 12
%jday of year - 001 to 366 %mmonth of year - 01 to 12
%Mminute - 00 to 59 %pstring containing AM or PM
%rtime as %I:%M:%S %p %Rtime as %H:%M
%Ssecond - 00 to 59 %Ttime as %H:%M:%S
%yyear within century - 00 to 99 %Yyear as CCYY (4 digits)
%Ztimezone name

Example:

1.
2.
3.
4.
5.
6.
7.
8.
<!--#echo var="DATE_LOCAL"-->
Saturday, 16-Dec-2017 21:00:02 Mountain Standard Time
<!--#config timefmt="%c"-->
<!--#echo var="DATE_LOCAL"-->
12/16/17 21:00:02
<!--#config timefmt="%A %B %e, %Y"-->
<!--#echo var="DATE_LOCAL"-->
Saturday December 16, 2017

Condtional SSI

Have you ever wanted to use conditional statements in Server Side Includes. Not many people know that SSI supports conditional statements.

<!--#if expr="expression"-->
<!--#elif expr="expression"-->
<!--#else-->
<!--#endif-->

The expression from the flow control code is one of the following:

string1If string is NOT empty result is true
string1=string2If string1 is equal to string2 result is true
string1!=string2If string1 is NOT equal to string2 result is true
string1If string1 is less than string2 result is true
string1>string2If string1 is greater than string2 result is true
string1>=string2If string1 greater than or equal to string2 result is true
(test_condition)If test_condition TRUE result is true
!test_conditionIf test_condition FALSE result is true
test_condition1 && test_condition2If test_condition1 AND test_condition2 are true result is true
test_condition1 || test_condition2If test_condition1 OR test_condition2 are true result is true

In this example, we are to "include" a specific iebar.htm page for Internet explorer. Any other browser will be defaultbar.htm.

Example:

1.
2.
3.
4.
5.
<!--#if expr="${HTTP_USER_AGENT} = /MSIE/" -->
<!--#include file="iebar.htm" -->
<!--#else -->
<!--#include file="defaultbar.htm" -->
<!--#endif -->

Time Limited Content using Conditional SSI

Have you ever had content you wanted to appear only between certain dates? Sure you could do this with a scripting language (PERL, PHP, etc...). But why not do it with something easy like SSI. Lets say, you have some sort of special going on, but you find yourself forgetting to take it down from your website once the special is over. The example below demonstrates how you can create content that only displays between 2 given dates.

You can also use this technique to display content at a future date.

Example:

This example will make the special valid from 01/01/2010 to 03/31/2010. You would code your date as YYYYMMDD. It is very important to code single digit months and days as 01, 02, 03, etc... To code the date for August 30, 2010, it would be "20100830".

1.
2.
3.
4.
5.
6.
<!--#config timefmt="%Y%m%d"-->
<!--#if expr="($DATE_LOCAL >= 20100101 && $DATE_LOCAL <= 20100331)"-->
We are having a special!
<!--#else-->
We are sorry, our special is now over.
<!--#endif-->

Page Changed and Copyright Date Footer

Simple directives to display the current year as the copyright year and display the date a page was last modified.

Example:

1.
2.
3.
4.
<!--#config timefmt="%Y" -->
Copyright 1994 - <!--#echo var="DATE_LOCAL" --> TWD, Inc.
<!--#config timefmt="%B %d %Y" -->
Page modified: <!--#echo var="LAST_MODIFIED" -->.

Output:

Copyright 1994 - 2017 TWD, Inc. Page modified: January 26 2012.

File Sizes

Simple way of displaying filesizes for downloads or other files.

Example:

1.
2.
3.
4.
5.
6.
<!--#config sizefmt="bytes"-->
The size of this Article is <!--#fsize file="index.html"--> Bytes
The size of this Article is 15,510 Bytes
<!--#config sizefmt="abbrev"-->
The size of this Article is <!--#fsize file="index.html"-->
The size of this Article is 15K

Published by: Thomas Penwell
Initially published on: May 12, 2010
Article last modified on: Thursday, January 26, 2012.

Home - Web Portfolio - Web site Tools - Database Design Service - Consulting
Web Design Service - Web Hosting Solutions - Service Rates
Client Login - Feedback Form - Promotions - Partners - Articles - Perl Scripts


Our Sites:
restaurantorderpad.com