Server side include embed technology SSI (Server SideInc lude

  • 2020-05-09 19:46:48
  • OfStack

SSI profile

SSI is very useful in static files and can separate variable modules such as daily leaderboards. Its main functions are:

1. Display server-side environment variables < #echo >
Insert text directly into the document < #include >
3. Display information about the WEB document < #flastmod #fsize > (e.g. date/size of document)
4. Directly execute various programs on the server < #exec > (e.g., CGI or other executable)
5. Set the SSI information display format < #config > (e.g. Date/size display)
6. Advanced SSI < XSSI > Variables can be set using if conditional statements. apache, nginx, etc., all support SSI command, so it is ok under configuration. The configuration of Nginx can be referred to:

http://wiki.nginx.org/HttpSsiModuleChs

The default extensions for SSI are.stm,.shtm, and.shtml

SSI grammar

Example:

< !--#command param="value"-- >

The syntax of SSI is very simple, but the following points should be noted when using it:

1. < ! There is no space between the copyright and #
2.SSI is case sensitive
3. All value should be written in quotation marks

SSI command

config command

The config command is mainly used to modify the default Settings of SSI, such as time format, default error message, and file size units.

Set the default error message: errmsg

<!--#config errmsg="Error,please contact webmaster@mail.com"-->

Define date and time format: timefmt
<!--#config timefmt="%A, %B %d, %Y"-->

Define file size units
<!--#config sizefmt="bytes"-->

The config command is only valid for subsequent commands. Also, the post-defined Settings have a higher priority and override the previous Settings.

include command

The include name is probably the most used command in SSI and the main function of SSI.
The Include command allows you to insert text or images from other documents into the currently parsed document. The Include command allows you to update your entire site instantly by changing only one file!

<!--#include virtual="/inc/header.inc"-->
<!--#include file="inc/desc.inc"-->

The include command supports importing files via virtual paths (virtual) and relative paths (file), regardless of the type of file referenced.

set command

Variables can be defined using set:

<!--#set var="blog" value="//www.ofstack.com"-->

After the variable is defined, it can be used:
<!--#echo var="blog"-->

Use environment variables when defining variables:
<!--#set var="fname" value="${DOCUMENT_NAME}${DOCUMENT_URI}"-->

If it is a single environment variable, it can be separated without using {} :
<!--#set var="fname" value="$DOCUMENT_NAME"-->

References to environment variables need to be prefixed with $; if $is used as a character only, escape with \$.

echo command

echo displays the values of variables, including custom variables and environment variables

<! � #echo var= " DOCUMENT_NAME " �  >

Note: environment variables used in the echo command do not require the $prefix. The main environment variables of SSI are as follows:

name description type
DOCUMENT_NAME 当前文档名 SSI
DOCUMENT_URI 当前文档虚拟路径 SSI
QUERY_STRING_UNESCAPED 未经转义处理的由客户端发送的查询字串,所有的特殊字符前面都有转义符”\” SSI
DATE_LOCAL 服务器设定时区的日期和时间 SSI
DATE_GMT 功能与DATE_LOCAL1样,但返回的是以格林尼治标准时间为基准的日期 SSI
LAST_MODIFIED 当前文档的最后更新时间 SSI
SERVER_SOFTWARE 服务器软件的名称和版本 CGI
SERVER_NAME 服务器的主机名称,DNS别名或IP地址 CGI
SERVER_PROTOCOL 客户端请求所使用的协议名称和版本 CGI
SERVER_PORT 服务器的响应端口 CGI
REMOTE_HOST 发出请求信息的客户端主机名称 CGI
REMOTE_ADDR 发出请求信息的客户端IP地址 CGI
AUTH_TYPE 用户身份的验证方法 CGI
REMOTE_USER 访问受保护页面的用户所使用的帐号名称 CGI

More environment variables can be viewed using the printenv command. The printenv command displays all environment variables

fsize command

Displays the size of the specified file in combination with config sizefmt to specify the output format.


<!-- Output the current document size -->
<!--#fsize file="$DOCUMENT_NAME"-->
<!--#fsize virtual="$DOCUMENT_URI"-->

flastmod command

Displays the last update date of the specified file, which can be combined with config sizefmt to specify the output format.


<!-- Output the current document size -->
<!--#flastmod file="$DOCUMENT_NAME"-->
<!--#flastmod virtual="$DOCUMENT_URI"-->

exec command

The Exec command can execute either the CGI script or the shell command. Usage:

1.CMD: executes the specified string using /bin/sh. If SSI USES the IncludesNOEXEC option, the command is blocked

2.CGI: can be used to execute CGI scripts

if... statements

In SSI, the conditional judgment statement if can also be used. The syntax is as follows:

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

Example:

<!--#if expr="$DOCUMENT_NAME=index.shtml"-->
<p> This is done by if Determine the current document name "index.shtml" After the show </p>
<!--#elif expr="$DOCUMENT_NAME=index.html"-->
<p> This is done by if Determine the current document name "index.html" After the show </p>
<!--#else -->
<p> neither "index.shtml" , nor is it "index.html"</p>
<!--#endif -->
demo


Related articles: