SSI (Server Side Include) in one page

转自:http://ssi.su/

Review
What are SSI?
SSI (Server Side Includes) are directives that are placed in HTML pages, and evaluated on the server while the pages are being served. They let you add dynamically generated content to an existing HTML page, without having to serve the entire page via a CGI program, or other dynamic technology.
The decision of when to use SSI, and when to have your page entirely generated by some program, is usually a matter of how much of the page is static, and how much needs to be recalculated every time the page is served. SSI is a great way to add small pieces of information, such as the current time. But if a majority of your page is being generated at the time that it is served, you need to look for some other solution.
Configuring server to permit SSI
To permit SSI on your server, you must have mod_include installed and enabled. Additionally, you must have the following directive either in your httpd.conf file, or in a .htaccess file:
Options +Includes
This tells server that you want to permit files to be parsed for SSI directives. Note that most configurations contain multiple Options directives that can override each other. You will probably need to apply the Options to the specific directory where you want SSI enabled in order to assure that it gets evaluated last.
Not just any file is parsed for SSI directives. You have to tell server which files should be parsed. There are two ways to do this. You can tell server to parse any file with a particular file extension, such as .shtml, with the following directives:
AddType text/html .shtml
AddHandler server-parsed .shtml
or, for version 2.2:
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
One disadvantage to this approach is that if you wanted to add SSI directives to an existing page, you would have to change the name of that page, and all links to that page, in order to give it a .shtml extension, so that those directives would be executed.
For backwards compatibility, documents with mime type text/x-server-parsed-html or text/x-server-parsed-html3 will also be parsed (and the resulting output given the mime type text/html).
The other method is to use the XBitHack directive:
XBitHack on
XBitHack tells Server to parse files for SSI directives if they have the execute bit set. So, to add SSI directives to an existing page, rather than having to change the file name, you would just need to make the file executable using chmod.
chmod +x pagename.html
A brief comment about what not to do. You'll occasionally see people recommending that you just tell Apache to parse all .html files for SSI, so that you don't have to mess with .shtml file names. These folks have perhaps not heard about XBitHack. The thing to keep in mind is that, by doing this, you're requiring that Apache read through every single file that it sends out to clients, even if they don't contain any SSI directives. This can slow things down quite a bit, and is not a good idea.
Of course, on Windows, there is no such thing as an execute bit to set, so that limits your options a little.
In its default configuration, Apache does not send the last modified date or content length HTTP headers on SSI pages, because these values are difficult to calculate for dynamic content. This can prevent your document from being cached, and result in slower perceived client performance. There are two ways to solve this:
  1. Use the XBitHack Full configuration. This tells Apache to determine the last modified date by looking only at the date of the originally requested file, ignoring the modification date of any included files.
  2. Use the directives provided by mod_expires to set an explicit expiration time on your files, thereby letting browsers and proxies know that it is acceptable to cache them.
Basic SSI Directives
Syntax of SSI Commands
The document is parsed as an HTML document, with special commands embedded as SGML comments. A command has the syntax:
<!--#element attribute=value attribute=value ... -->
It is formatted like an HTML comment, so if you don't have SSI correctly enabled, the browser will ignore it, but it will still be visible in the HTML source. If you have SSI correctly configured, the directive will be replaced with its results.
The value will often be enclosed in double quotes; many commands only allow a single attribute-value pair. Note that the comment terminator (-->) should be preceded by whitespace to ensure that it isn't considered part of an SSI token. Note that the leading <!--# is one token and may not contain any whitespaces.
The element can be one of a number of things, and we'll talk some more about most of these in the next installment of this series. For now, here are some examples of what you can do with SSI
Today's date
<!--#echo var="DATE_LOCAL" -->
The echo element just spits out the value of a variable. There are a number of standard variables, which include the whole set of environment variables that are available to CGI programs. Also, you can define your own variables with the set element.
If you don't like the format in which the date gets printed, you can use the config element, with a timefmt attribute, to modify that formatting.
<!--#config timefmt="%A %B %d, %Y" -->
Today is <!--#echo var="DATE_LOCAL" -->
Modification date of the file
This document last modified <!--#flastmod file="index.html" -->
This element is also subject to timefmt format configurations.
Including the results of a CGI program
This is one of the more common uses of SSI - to output the results of a CGI program, such as everybody's favorite, a ``hit counter.''
<!--#include virtual="/cgi-bin/counter.pl" -->
SSI Commands (SSI Directives)
config
This command controls various aspects of the parsing.
errmsg
The value is a message that is sent back to the client if an error occurs whilst parsing the document.
sizefmt
The value sets the format to be used which displaying the size of a file. Valid values are bytes for a count in bytes, or abbrev for a count in Kb or Mb as appropriate.
timefmt
The value is a string to be used by the strftime(3) library routine when printing dates.
Example: What else can I config?
In addition to being able to config the time format, you can also config two other things.
Usually, when something goes wrong with your SSI directive, you get the message
[an error occurred while processing this directive]
If you want to change that message to something else, you can do so with the errmsg attribute to the config element:
<!--#config errmsg="[It appears that you don't know how to use SSI]" -->
Hopefully, end users will never see this message, because you will have resolved all the problems with your SSI directives before your site goes live. (Right?)
And you can config the format in which file sizes are returned with the sizefmt attribute. You can specify bytes for a full count in bytes, or abbrev for an abbreviated number in Kb or Mb, as appropriate.
echo
This command prints one of the include variables, defined below. If the variable is unset, it is printed as (none). Any dates printed are subject to the currently configured timefmt.
var
The value is the name of the variable to print.
encoding
Specifies how Apache should encode special characters contained in the variable before outputting them. If set to "none", no encoding will be done. If set to "url", then URL encoding (also known as %-encoding; this is appropriate for use within URLs in links, etc.) will be performed. At the start of an echo element, the default is set to "entity", resulting in entity encoding (which is appropriate in the context of a block-level HTML element, eg. a paragraph of text). This can be changed by adding an encoding attribute, which will remain in effect until the next encoding attribute is encountered or the element ends, whichever comes first. Note that the encoding attribute must precede the corresponding var attribute to be effective, and that only special characters as defined in the ISO-8859-1 character encoding will be encoded. This encoding process may not have the desired result if a different character encoding is in use. Apache 1.3.12 and above; previous versions do no encoding.
Example: When was this document modified?
<!--#config timefmt="%D" -->
This file last modified <!--#echo var="LAST_MODIFIED" -->
exec
The exec command executes a given shell command or CGI script. The IncludesNOEXEC Option disables this command completely.
cgi
The value specifies a (%-encoded) URL relative path to the CGI script. If the path does not begin with a (/), then it is taken to be relative to the current document. The document referenced by this path is invoked as a CGI script, even if the server would not normally recognize it as such. However, the directory containing the script must be enabled for CGI scripts (with ScriptAlias or the ExecCGI Option).
The CGI script is given the PATH_INFO and query string (QUERY_STRING) of the original request from the client; these cannot be specified in the URL path. The include variables will be available to the script in addition to the standard CGI environment.
For example:
<!--#exec cgi="/cgi-bin/example.cgi" -->
If the script returns a Location: header instead of output, then this will be translated into an HTML anchor.
The include virtual element should be used in preference to exec cgi. In particular, if you need to pass additional arguments to a CGI program, using the query string, this cannot be done with exec cgi, but can be done with include virtual, as shown here:
<!--#include virtual="/cgi-bin/example.cgi?argument=value" -->
cmd
The server will execute the given string using /bin/sh. The include variables are available to the command, in addition to the usual set of CGI variables.
The use of #include virtual is almost always prefered to using either #exec cgi or #exec cmd. The former (#include virtual) used the standard Apache sub-request mechanism to include files or scripts. It is much better tested and maintained.
In addition, on some platforms, like Win32, and on unix when using suexec, you cannot pass arguments to a command in an exec directive, or otherwise include spaces in the command. Thus, while the following will work under a non-suexec configuration on unix, it will not produce the desired result under Win32, or when running suexec:
<!--#exec cmd="perl /path/to/perlscript arg1 arg2" -->
Example: Executing commands
I expect that I'll have an article some time in the coming months about using SSI with small CGI programs. For now, here's something else that you can do with the exec element. You can actually have SSI execute a command using the shell (/bin/sh, to be precise - or the DOS shell, if you're on Win32). The following, for example, will give you a directory listing.
<pre>
  <!--#exec cmd="ls" -->
</pre>
or, on Windows
<pre>
  <!--#exec cmd="dir" -->
</pre>
You might notice some strange formatting with this directive on Windows, because the output from dir contains the string ``<dir>'' in it, which confuses browsers.
Note that this feature is exceedingly dangerous, as it will execute whatever code happens to be embedded in the exec tag. If you have any situation where users can edit content on your web pages, such as with a ``guestbook'', for example, make sure that you have this feature disabled. You can allow SSI, but not the exec feature, with the IncludesNOEXEC argument to the Options directive.
fsize
This command prints the size of the specified file, subject to the sizefmt format specification. Attributes:
file
The value is a path relative to the directory containing the current document being parsed.
virtual
The value is a (%-encoded) URL-path relative to the current document being parsed. If it does not begin with a slash (/) then it is taken to be relative to the current document.
flastmod
This command prints the last modification date of the specified file, subject to the timefmt format specification. The attributes are the same as for the fsize command.
Example: When was this document modified?
Earlier, we mentioned that you could use SSI to inform the user when the document was most recently modified. However, the actual method for doing that was left somewhat in question. The following code, placed in your HTML document, will put such a time stamp on your page. Of course, you will have to have SSI correctly enabled, as discussed above.
<!--#config timefmt="%A %B %d, %Y" -->
This file last modified <!--#flastmod file="ssi.shtml" -->
include
This command inserts the text of another document or file into the parsed file. Any included file is subject to the usual access control. If the directory containing the parsed file has the Option IncludesNOEXEC set, and the including the document would cause a program to be executed, then it will not be included; this prevents the execution of CGI scripts. Otherwise CGI scripts are invoked as normal using the complete URL given in the command, including any query string.
An attribute defines the location of the document; the inclusion is done for each attribute given to the include command.
file
The value is a path relative to the directory containing the current document being parsed. It cannot contain ../, nor can it be an absolute path. Therefore, you cannot include files that are outside of the document root, or above the current document in the directory structure. The virtual attribute should always be used in preference to this one.
virtual
The value is a (%-encoded) URL relative to the current document being parsed. The URL cannot contain a scheme or hostname, only a path and an optional query string. If it does not begin with a slash (/) then it is taken to be relative to the current document.
A URL is constructed from the attribute, and the output the server would return if the URL were accessed by the client is included in the parsed output. Thus included files can be nested.
If the specified URL is a CGI program, the program will be executed and its output inserted in place of the directive in the parsed file. You may include a query string in a CGI url:
<!--#include virtual="/cgi-bin/example.cgi?argument=value" -->
include virtual should be used in preference to exec cgi to include the output of CGI programs into an HTML document.
Example: Including a standard footer
If you are managing any site that is more than a few pages, you may find that making changes to all those pages can be a real pain, particularly if you are trying to maintain some kind of standard look across all those pages.
Using an include file for a header and/or a footer can reduce the burden of these updates. You just have to make one footer file, and then include it into each page with the include SSI command. The include element can determine what file to include with either the file attribute, or the virtual attribute. The file attribute is a file path, relative to the current directory. That means that it cannot be an absolute file path (starting with /), nor can it contain ../ as part of that path. The virtual attribute is probably more useful, and should specify a URL relative to the document being served. It can start with a /, but must be on the same server as the file being served.
<!--#include virtual="/footer.html" -->
I'll frequently combine the last two things, putting a LAST_MODIFIED directive inside a footer file to be included. SSI directives can be contained in the included file, and includes can be nested - that is, the included file can include another file, and so on.
printenv
This prints out a listing of all existing variables and their values. Starting with Apache 1.3.12, special characters are entity encoded (see the echo element for details) before being output. There are no attributes.
For example:
<!--#printenv -->>
The printenv element is available only in Apache 1.2 and above.
set
This sets the value of a variable.
var
The name of the variable to set.
value
The value to give a variable.
For example:
<!--#set var="category" value="help" -->
The set element is available only in Apache 1.2 and above.
Examples: Setting variables
Using the set directive, you can set variables for later use. We'll need this later in the discussion, so we'll talk about it here. The syntax of this is as follows:
<!--#set var="name" value="Rich" -->
In addition to merely setting values literally like that, you can use any other variable, including environment variables or the variables discussed above (like LAST_MODIFIED, for example) to give values to your variables. You will specify that something is a variable, rather than a literal string, by using the dollar sign ($) before the name of the variable.
<!--#set var="modified" value="$LAST_MODIFIED" -->
To put a literal dollar sign into the value of your variable, you need to escape the dollar sign with a backslash.
<!--#set var="cost" value="\$100" -->
Finally, if you want to put a variable in the midst of a longer string, and there's a chance that the name of the variable will run up against some other characters, and thus be confused with those characters, you can place the name of the variable in braces, to remove this confusion. (It's hard to come up with a really good example of this, but hopefully you'll get the point.)
<!--#set var="date" value="${DATE_LOCAL}_${DATE_GMT}" -->
Include Variables
In addition to the variables in the standard CGI environment, these are available for the echo command, for if and elif, and to any program invoked by the document.
DATE_GMT
The current date in Greenwich Mean Time.
DATE_LOCAL
The current date in the local time zone.
DOCUMENT_NAME
The filename (excluding directories) of the document requested by the user.
DOCUMENT_URI
The (%-decoded) URL path of the document requested by the user. Note that in the case of nested include files, this is not then URL for the current document.
LAST_MODIFIED
The last modification date of the document requested by the user.
USER_NAME
Contains the owner of the file which included it.
Variable Substitution
Variable substitution is done within quoted strings in most cases where they may reasonably occur as an argument to an SSI directive. This includes the config, exec, flastmod, fsize, include, and set directives, as well as the arguments to conditional operators. You can insert a literal dollar sign into the string using backslash quoting:
<!--#if expr="$a = \$test" -->
If a variable reference needs to be substituted in the middle of a character sequence that might otherwise be considered a valid identifier in its own right, it can be disambiguated by enclosing the reference in braces, à la shell substitution:
<!--#set var="Zed" value="${REMOTE_HOST}_${REQUEST_METHOD}" -->
This will result in the Zed variable being set to "X_Y" if REMOTE_HOST is "X" and REQUEST_METHOD is "Y".
EXAMPLE: the below example will print "in foo" if the DOCUMENT_URI is /foo/file.html, "in bar" if it is /bar/file.html and "in neither" otherwise:
<!--#if expr="\"$DOCUMENT_URI\" = \"/foo/file.html\"" -->
in foo
<!--#elif expr="\"$DOCUMENT_URI\" = \"/bar/file.html\"" -->
in bar
<!--#else -->
in neither
<!--#endif -->
Flow Control Elements
These are available in Apache 1.2 and above. The basic flow control elements are:
<!--#if expr="test_condition" -->
<!--#elif expr="test_condition" -->
<!--#else -->
<!--#endif -->
The if element works like an if statement in a programming language. The test condition is evaluated and if the result is true, then the text until the nextelif, else or endif element is included in the output stream.
The elif or else statements are be used the put text into the output stream if the originaltest_condition was false. These elements are optional.
The endif element ends the if element and is required.
test_condition is one of the following:
string
true if string is not empty
string1 = string2
string1 != string2
string1 < string2
string1 <= string2
string1 > string2
string1 >= string2
Compare string1 with string2. If string2 has the form /string/ then it is compared as a regular expression. Regular expressions have the same syntax as those found in the Unix egrep command.
( test_condition )
true if test_condition is true
! test_condition
true if test_condition is false
test_condition1 && test_condition2
true if both test_condition1 and test_condition2 are true
test_condition1 || test_condition2
true if either test_condition1 or test_condition2 is true
"=" and "!=" bind more tightly than "&&" and "||". "!" binds most tightly. Thus, the following are equivalent:
<!--#if expr="$a = test1 && $b = test2" -->
<!--#if expr="($a = test1) && ($b = test2)" -->
Anything that's not recognized as a variable or an operator is treated as a string. Strings can also be quoted: 'string'. Unquoted strings can't contain whitespace (blanks and tabs) because it is used to separate tokens such as variables. If multiple strings are found in a row, they are concatenated using blanks. So,
string1 string2 results in string1 string2
'string1 string2' results in string1 string2
XBitHack directive
The XBitHack directives controls the parsing of ordinary html documents. This directive only affects files associated with the MIME type text/html.
off
No special treatment of executable files
on
Any file that has the user-execute bit set will be treated as a server-parsed html document.
full
As for on but also test the group-execute bit. If it is set, then set the Last-modified date of the returned file to be the last modified time of the file. If it is not set, then no last-modified date is sent. Setting this bit allows clients and proxies to cache the result of the request.
you would not want to use this, for example, when you #include a CGI that produces different output on each hit (or potentially depends on the hit).
SSI Links
Documentation From Web Servers Tutorials
  • Apache Tutorial: Introduction to Server Side Includes (Version 1.3) [http://httpd.apache.org/docs/1.3/howto/ssi.html]>>
  • Apache Tutorial: Introduction to Server Side Includes (Version 2.2) [http://httpd.apache.org/docs/2.2/howto/ssi.html]>>
  • Apache Tutorial: Module mod_include (Version 1.3) [http://httpd.apache.org/docs/1.3/mod/mod_include.html]>>
  • Apache Tutorial: Module mod_include (Version 2.2) [http://httpd.apache.org/docs/2.2/mod/mod_include.html]>>
  • Jigsaw (W3C's Server) Tutorial: Server Side Include commands [http://www.w3.org/Jigsaw/Doc/User/SSI.html]>>

阅读更多
换一批

没有更多推荐了,返回首页