This page has been robot translated, sorry for typos if any. Original content here.

SSI in the examples

First of all, the reasons that prompted me to write this article: discussions of the members of the All-Russian Club of Webmasters have often argued about what is better to apply in case of repeated markup (information) on a large number of pages - frames, JavaScript or SSI (Server Side Includes). My deep conviction is that frames should be used only where there is really no way to do without it, JavaScript (and any other client scripts) is too capricious and depends on browser settings, so it can only be used for additional features, but not for construction, for example, navigation systems. I will not argue with foam at the mouth that SSI technology is steeper than all mountains and eggs, but I will simply give solutions to problems that often occur in practice with the use of this technology.

(Immediately foresee the indignation of the adherents of ASP and PHP technologies, with which solutions of similar tasks are also possible, therefore, especially for them: the SSI technology is much simpler, it only has a dozen operations, so for a non-programmer this is a better choice, if only because it can be quickly mastered)

Date of the last modification of the document

Simple Directive:

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

Today's number in the required format

If we need to display the date not in the standard form for the configuration of the software, but what we need (for example, "Tuesday, May 30, 2000"), we can use the following design: Getting the day of the week

  <! - # config timefmt = "% u" ->
 <! - # set var = "NUM_DAY" value = "$ DATE_LOCAL" ->
 <! - # if expr = "$ NUM_DAY = 1" ->
 <! - # set var = "DAY" value = "Monday" ->
 <! - # elif expr = "$ NUM_DAY = 2" ->
 <! - # set var = "DAY" value = "Tuesday" ->
 <! - # elif expr = "$ NUM_DAY = 3" ->
 <! - # set var = "DAY" value = "environment" ->

 <! - # elif expr = "$ NUM_DAY = 4" ->
 <! - # set var = "DAY" value = "Thursday" ->
 <! - # elif expr = "$ NUM_DAY = 5" ->
 <! - # set var = "DAY" value = "Friday" ->
 <! - # elif expr = "$ NUM_DAY = 6" ->
 <! - # set var = "DAY" value = "Saturday" ->
 <! - # else ->
 <! - # set var = "DAY" value = "Sunday" ->
 <! - # endif -> 

Getting the number

  <! - # config timefmt = "% e" ->
 <! - # set var = "DATE" value = "$ DATE_LOCAL" -> 

Getting the name of the month

  <! - # config timefmt = "% m" ->
 <! - # set var = "NUM_MONTH" value = "$ DATE_LOCAL" ->
 <! - # if expr = "$ NUM_MONTH = 01" ->
 <! - # set var = "MONTH" value = "January" ->
 <! - # elif expr = "$ NUM_MONTH = 02" ->

 <! - # set var = "MONTH" value = "February" ->
 <! - # elif expr = "$ NUM_MONTH = 03" ->
 <! - # set var = "MONTH" value = "March" ->
 <! - # elif expr = "$ NUM_MONTH = 04" ->
 <! - # set var = "MONTH" value = "April" ->
 <! - # elif expr = "$ NUM_MONTH = 05" ->
 <! - # set var = "MONTH" value = "May" ->
 <! - # elif expr = "$ NUM_MONTH = 06" ->
 <! - # set var = "MONTH" value = "June" ->

 <! - # elif expr = "$ NUM_MONTH = 07" ->
 <! - # set var = "MONTH" value = "July" ->
 <! - # elif expr = "$ NUM_MONTH = 08" ->
 <! - # set var = "MONTH" value = "August" ->
 <! - # elif expr = "$ NUM_MONTH = 09" ->
 <! - # set var = "MONTH" value = "September" ->
 <! - # elif expr = "$ NUM_MONTH = 10" ->
 <! - # set var = "MONTH" value = "October" ->
 <! - # elif expr = "$ NUM_MONTH = 11" ->

 <! - # set var = "MONTH" value = "November" ->
 <! - # else ->
 <! - # set var = "MONTH" value = "December" ->
 <! - # endif -> 

Obtaining the year

  <! - # config timefmt = "% G" ->
 <! - # set var = "YEAR" value = "$ DATE_LOCAL" -> 

The actual output of the resulting string

  <! - # echo var = "DAY" ->,

 <! - # echo var = "DATE" ->
 <! - # echo var = "MONTH" ->,
 <! - # echo var = "YEAR" ->

Parameters for config timefmt should be looked at for each configuration of the web server separately. The above example is FreeBSD, Apache. For more information, see man timefmt
A similar design is used on the website of the online store Levingston.Ru

Date of modification of external file

Often on computer sites spread a price list in MS Excel or Word format and each time they write down the date of its manufacture with their hands. Using SSI, this is done approximately as follows:

  <a href=pricelst.doc> Pricelist </a>

 <! - # config timefmt = "% d.% m.% y" ->
 (MS Word 6.0 / 95, <! - # flastmod virtual = "pricelst.doc" ->) 

This design is used on the Duncan Service website

The fight against <noframes>

Typically, in this container write "sorry, but you should update the browser," in theory there should be an alternative for users of older versions of browsers. Since on a normal server the information is often changed, and the webmaster is lazy every time to make edits in two places. With SSI, the problem is solved once and for all: a directive is inserted into the <noframes> </ noframes> container, inserting the same file in which the edits or simply the navigation bar are made.

Fighting direct links to documents on a Web site with frames

One of the arguments against the use of frame structures when creating Web sites is the inconvenience of direct links to content files. For example, when you link from search engines or to a specific (non-root) document from another Web site, the user hits a page that lacks design or navigation elements, which are usually placed in a separate navigation frame. With the help of a simple SSI design, this problem can be solved. To do this, you need to analyze where the user came from (HTTP_REFERER variable). If he came not from our server, but from outside - to construct the frame structure and substitute the document requested by the user as a meaningful frame.

In the example below, the file content.html is the document that is directly referenced (say, from the search engine), frame.html is the file in which the frame structure is built. QUERY_STRING substitutes the value done in order to avoid the infinite nesting of frame structures.

File content.html

  <html>
 <head>
 ...
 <! - # include virtual = "frame.html" ->
 </ head>
 <body>

 ...
 </ body>
 </ html> 

File frame.html

  <! - # if expr = "$ QUERY_STRING! = done && $ HTTP_REFERER! = / your_domain \ .ru /" ->
 <frameset rows = "150, *">
 <frame name = "NAVIGATION" src = "/ navigation.html">
 <frame name = "CONTENT" src = "<! - # echo var =" DOCUMENT_URI "->? done">

 </ frameset>
 <! - # endif -> 

A similar design is used on the website of the online store Bolero

Version of the page for printing

Often there is an applied problem - a beautiful multi-column design with top and bottom caps, a cloud of banners, but when you print it all is not necessary - excess paper, unnecessary information ... Therefore, I want to make a simple alternative page view specifically for printing. To do this, it is enough to prepare two versions of the top and bottom caps, one for screen display, the other for printing. As a switch between these options, use the variable QUERY_STRING. Below are the basic structures for the page itself (file.html) and for the top and bottom caps (top.html and bottom.html).

Structure of the page itself (file.html):

  <! - # include virtual = "top.html? $ QUERY_STRING" ->
 here the body of the document
 <! - # include virtual = "bottom.html? $ QUERY_STRING" -> 

Structure top.html and bottom.html

  <! - # if expr = "$ QUERY_STRING == / for_printing /" ->
 print cap
 <! - # else ->
 header for viewing
 <! - # endif -> 

The link on each page should be of the form

  <a href=<"!--echo var="$DOCUMENT_URI" -->? for_printing
 > printable version </a> 

A similar design is used on the Web site of the All-Russian Club of Webmasters

Fighting menu items

Suppose we have several sections of the Web site, the documents relating to the sections are in different directories. The task is to make the reference to the section in which the user is currently located in the navigation menu on these sections (or not highlighted, highlighted in a different color, etc.). To do this, you can use the variable DOCUMENT_URI.

  <! - # if expr = "$ DOCUMENT_URI! = / ^ \ / index.html /" ->
 <a href="/"> First page </a> <br>
 <! - # endif ->

 <! - # if expr = "$ DOCUMENT_URI! = / \ / about \ /index.html/" ->
 <a href="/about/"> About Us </a> <br>
 <! - # endif ->
 <! - # if expr = "$ DOCUMENT_URI! = / \ / done \ /index.html/" ->
 <a href="/done/"> Our work </a> <br>
 <! - # endif ->

 <! - # if expr = "$ DOCUMENT_URI! = / \ / partner \ /index.html/" ->
 <a href="/partner/"> Our Partners </a> <br>
 <! - # endif ->
 <! - # if expr = "$ DOCUMENT_URI! = / \ / client \ /index.html/" ->
 <a href="/client/"> Our customers </a> <br>
 <! - # endif ->

 <! - # if expr = "$ DOCUMENT_URI! = / \ / price \ /index.html/" ->
 <a href="/price/"> Our prices </a> <br>
 <! - # endif ->
 <! - # if expr = "$ DOCUMENT_URI! = / \ / contacts \ /index.html/" ->
 <a href="/contacts/"> Our coordinates </a> <br>
 <! - # endif ->

 <! - # if expr = "$ DOCUMENT_URI! = / \ / history \ /index.html/" ->
 <a href="/history/"> Our Story </a> <br>
 <! - # endif ->
 <! - # if expr = "$ DOCUMENT_URI! = / \ / search.html /" ->
 <a href="/search.html"> Search </a> <br>
 <! - # endif -> 

A similar design is used by the Web 2000 Web site

Contextual text output depending on conditions

Suppose we have a task to change the appearance of the first page of the node, depending on where the user came from. To implement this task, the following construction is used:

  <! - # if expr = "$ HTTP_REFERER = / www.zzz.ru /" ->
 Here the necessary actions are taken
 <! - # endif -> 

those. the variable HTTP_REFERER is compared

Combating different versions of the design for different browsers

Everyone knows that browsers of different manufacturers and versions differently display the same HTML-markup of the document, starting from special tags and attributes and ending with support for different versions of JavaScript. In order for the Web site to look fine for one version of the browser and for the other, or to avoid JavaScript errors using SSI, you can check for versions or browsers and output various HTML markup options. For this, the HTTP_USER_AGENT variable is analyzed, which contains information about the type and manufacturer of the browser:

  <! - # if expr = "$ HTTP_USER_AGENT = / Mozilla \ / 4 / || $ HTTP_USER_AGENT = / Mozilla \ / 5 /" ->
 If the browser version is not the same as 4 or 5, then output the option
 design, using, for example, DHTML
 <! - # else ->
 Here you can get a simple design
 <! - # endif ->

A similar construction was used to build the frame structure and display the internal menu (for Netscape - for DHTML, for MSIE - for ActiveX) on the Web site of the magazine "OM"

The struggle with the design of the results of the work of scripts

The biggest problem with scripts lies in the fact that if there is a large amount of software, when changing the design, they need to be reconfigured. Well, if the software is done correctly and changing the display templates does not concern the mathematics itself, however, in such cases there are problems. For example, on the search page it would be nice to twist banners and ad networks, but SSI directives are not implemented in .cgi files. You can solve such problems using SSI as follows: do not process the display templates with the script, but call the script from the HTML document via SSI (unfortunately, this way you can work only with those scripts that use the GET method - the CONTENT_LENGTH variable is not available in SSI) So , as it is done there is an HTML document that is labeled in a general design style. It inserts a directive

  <! - # include virtual = "/ cgi-bin / script.cgi? $ QUERY_STRING" -> 

In the first step, while QUERY_STRING is empty - the first step of the script is invoked, in the subsequent steps of the script - the necessary parameters are transferred. In the script itself, three lines are important:

  ...
 # definition of the address where to send the data
 $ query = $ ENV {QUERY_STRING};
 # definition of the place from which the script was called
 $ uri = $ ENV {DOCUMENT_URI};
 ...
 # sending data to the same HTML document from which the script was called
 print "<form action = $ uri method = get> \ n";
 ... 

A similar design was used in the " Votings " section for the online supermarket Levingston.Ru

What can I do using Cookie and SSI?

There is often a JavaScript construct that displays "Hello, Ivan Ivanych!" when you visit the page of the Web site. The same can be done with the SSI directive

  <! - # echo var = "HTTP_COOKIE" -> 

Banners, caching and SSI

Sent by Kiril Khlopov (NewTech)

As you know, banner systems offer to include in the code some random value in the URL of the display script - protection against caching.

  • You can generate the entire page with a script, from the first to the last character
  • You can do as advised pages help banner systems - include by SSI script, generating a random banner
  • Dynamically record part of the document with the JavaScrip banner code

And it is possible still so:

  <! - # config timefmt = "% s" ->

 <! - # set var = "RND" value = "$ DATE_LOCAL" ->
 <! - Russian LinkExchange code START ->
 <iframe src = http: //www.linkexchange.ru/cgi-bin/erle.cgi? some_id?
 <! - # echo var = "RND" ->
 frameborder = 0 vspace = 0 hspace = 0 width = 468 height = 60 marginwidth = 0
  marginheight = 0 scrolling = no>
 <a href=http://www.linkexchange.ru/users/some_id/goto.map target=_top>
 <img src = http: //www.linkexchange.ru/cgi-bin/rle.cgi ?? <! - # echo var = "RND" ->
 alt = "RLE Banner Network" border = 0 height = 60 width = 468> </a>

 </ iframe>
 <! - Russian LinkExchange code END -> 

That is, how a random variable will perform the number of seconds from 1970. The time is not wasted and the memory for running the script generator, Java-Script (as among some users now it's fashionable) can be turned off. In my opinion it is quite rosy Smile happy
All this is checked and used by our administrator on www.j2.ru

One display template - different content

Sent by Eugene Bespalchikov (KADIS)

Often templates use this way: <> there is only one file that describes the structure of the page, and the main content is included by the directive

  <! - # include virtual = "$ QUERY_STRING.html" -> 
links, respectively, will look like:
  href = "www.your_domain.com/index.html?page1"
 href = "www.your_domain.com/index.html?page2"
 ... 

The problem arises if the user dials the address directly http://www.your_domain.ru, i.e. QUERY_STRING = ""

Decision:

  <! - # if expr = "$ QUERY_STRING" ->
 <! - # include virtual = "$ QUERY_STRING.html" ->
 <! - # else ->
 <! - # include virtual = "default.html" ->

 <! - # endif -> 

where default.html is the page of the root index (table of contents) and just a stub.