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

SSI in examples

To begin with about the reasons that prompted me to write this article: in the discussions of members of the All-Russian Webmasters Club, there were more than once disputes about what is better to use in the 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 you really can't do without it, JavaScript (and any other client scripts) is too capricious and depends on the browser settings, so it can only be used for additional features, but not for building, for example, navigation systems. I will not argue that the SSI technology is steeper than all the mountains and eggs, but simply will provide solutions to frequently encountered practical problems using this technology.

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

Last Modified Date

Simple directive:

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

Today's number in the right format

If we need to display the date not in the standard form for this software configuration, but in what we need (for example, "Tuesday, May 30, 2000"), then we can use the following construction: 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 -> 

Getting 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" ->

The parameter formats for the config timefmt must be viewed for each web server configuration separately. The example given is FreeBSD, Apache. See man timefmt for more details.
This design is used on the website of the online store Levingston. Ru

Date modified external file

Often on computer sites lay out the price list in MS Excel or Word format and each time they prescribe the date of its manufacture. With SSI, this is done roughly as follows:

  <a href=pricelst.doc> Price List </a>

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

This design is used on the Duncan Service website.

Fighting <noframes>

As a rule, in this container they 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 information changes frequently, and the webmaster is lazy to make edits in two places each time. With the help of SSI, the problem is solved once and for all: a directive is inserted into the <noframes> </ noframes> container, which inserts the same file in which edits are made or simply a navigation bar.

Fighting direct links to documents on a website with frames

One of the arguments against the use of frame structures when creating websites is the inconvenience of direct links to content files. For example, when linking from search engines or to a specific (non-root) document from another Web site, the user gets to a page without 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, it is necessary to analyze where the user came from (HTTP_REFERER variable). If it came not from our server, but from the outside, build a frame structure and substitute the document requested by the user as a meaningful frame.

In the example below, the content.html file is the document on which there is a direct link (say, from a search engine), frame.html is the file in which the frame structure is built. The value of done is substituted in QUERY_STRING in order to avoid infinite nesting of frame structures.

Content.html file

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

 ...
 </ body>
 </ html> 

Frame.html file

  <! - # 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 -> 

This design is used on the website of the online store Bolero

Print version page

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

The structure of the page itself (file.html):

  <! - # include virtual = "top.html? $ QUERY_STRING" ->
 here is 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 ->
 hat for viewing
 <! - # endif -> 

The link on each page should be

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

This design is used on the website of the All-Russian Webmasters Club.

Fighting menu items

Suppose we have several sections of the Web site, the documents related to the sections are in different directories. The task is to make it so that in the navigation menu for these sections disappears (or is not highlighted, highlighted in a different color, etc.) link to the section in which the user is currently located. 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 Works </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 -> 

This design is used by the Web 2000 website.

Contextual text output depending on conditions

Suppose we have the task of changing the appearance of the first page of the site, depending on where the user came from. To accomplish this task, the following construction is used:

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

those. compare variable HTTP_REFERER

Fighting different versions of design for different browsers

Everyone knows that browsers from different manufacturers and versions display the same HTML document markup differently, ranging from special tags and attributes to support for different versions of JavaScript. To make the website look normal for one browser version and another, or to avoid JavaScript errors using SSI, you can check versions or browsers and display different versions of HTML markup. 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 4 or 5, then display the option
 design using for example DHTML
 <! - # else ->
 Print simple design here.
 <! - # endif ->

A similar design was used to build the frame structure and display the internal menu (for Netscape - on DHTML, for MSIE - on ActiveX) on the OM web site

The fight against the design of the results of the scripts

The biggest problem with scripts is that if there is a large amount of software gained, changing their design requires reconfiguring them. It is good if the software is done correctly and changing the display patterns does not concern the mathematics itself, however, in such cases there are problems. For example, on the search page it would be nice to run banners and ad networks, but SSI directives are not processed in the .cgi files. Similar problems can be solved using SSI as follows: do not use a script to process display templates, but to call a script from an 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) How it is done There is an HTML document that is marked up in a general design style. The directive is inserted into it.

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

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

  ...
 # determine the address where to send data
 $ query = $ ENV {QUERY_STRING};
 # determining 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 is used in the "Voting" section for the online supermarket Levingston.Ru

What can be done using Cookie and SSI

Often found construction on JavaScript, which displays "Hello, Ivan Ivanovich!" when entering a website page. The same can be done using 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 variable in the URL of the display script - protection against caching.

  • You can generate an entire page with a script, from the first to the last character.
  • Can be done as advised by the help page banner systems - include by SSI script, generating a random banner
  • Dynamically record part of the document with the JavaScriper's banner code

And you can still like this:

  <! - # 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, the number of seconds from 1970 will appear as a random variable. Time is not wasted and the memory for launching a script generator, Java-Script (as is now fashionable among some users) can be turned off. In my opinion, quite rosy Smile happy
All this is tested and applied by our administrator on www.j2.ru

One display template - different content

Sent by Evgeny Bespalchikov (KADIS)

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

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

The problem occurs if the user types 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 root index page (table of contents) and is just a stub.