Home > Backend Development > PHP Tutorial > Detailed explanation of SSI usage (1)_PHP tutorial

Detailed explanation of SSI usage (1)_PHP tutorial

WBOY
Release: 2016-07-13 17:19:41
Original
1244 people have browsed it

你是否曾经或正在为如何能够在最短的时间内完成对一个包含上千个页面的网站的修改而苦恼?那么可以看一下本文的介绍,或许能够对你有所帮助。

什么是SSI?

  SSI是英文Server Side Includes的缩写,翻译成中文就是服务器端包含的意思。从技术角度上说,SSI就是HTML文件中,可以通过注释行调用的命令或指针。SSI 具有强大的功能,只要使用一条简单的SSI 命令就可以实现整个网站的内容更新,动态显示时间和日期,以及执行shell和CGI脚本程序等复杂的功能。SSI 可以称得上是那些资金短缺、时间紧张、工作量大的网站开发人员的最佳帮手。

  SSI最初在NCSA服务器平台上推出,在Apache服务器中得到扩展和加强,目前已经可以在几乎所有的服务器上运行。本文将主要结合Apache服务器介绍SSI的使用方法。

如何启动SSI?

  在Apache服务器下,可以通过直接编辑服务器配置文件或者在需要使用SSI的目录中创建.htaccess文件启动SSI。具体来说,过程如下:

1.服务器配置文件

  如果用户具有对服务器配置文件的访问权限,可以通过编辑文件access.conf和srm.conf启动SSI。

  首先使用Telnet远程登录服务器,找到配置文件的存放目录。一般来说,Apache服务器的配置文件都被保存在“/usr/local/etc/httpd/conf”目录下。使用任何一种文字编辑器打开文件srm.conf,找到以下几行:

# If you want to use server side includes, or CGI outside
# ScriptAliased directories, uncomment the following lines.
#AddType text/x-server-parsed-html .shtml

#AddType application/x-httpd-CGI .CGI

  用户的配置文件中可能没有上述的注释指令行,但是只要找到以AddType开头的两行并且去掉每一行最前面的“#”符号即可。

  保存所做的修改,然后再打开文件access.conf。用户需要在文件中找到设置DocumentRoot(根文件)的部分。一般来说该段文本如下,但是不排除$#@60;Directory$#@62;和$#@60;/Directory$#@62;标签之间还包含其它设置内容。

# This should be changed to whatever you set DocumentRoot to.
$#@60;Directory /usr/local/etc/httpd/htdocs$#@62;
# This may also be "None", "All", or any combination of "Indexes",
Options Indexes FollowSymLinks Includes
$#@60;/Directory$#@62;

  如果用户不希望执行脚本或shell命令,可以在options选项行中加入关键字IncludesNOEXEC,这样可以允许SSI,但是不能执行CGI或脚本命令。(注:最新版本的Apache服务器只有一个配置文件httpd.conf,以上所提到的内容都已经被包含在该文件中)

2.创建文件.htaccess

  如果用户不能直接访问服务器配置文件,可以使用文件编辑器创建一个名为.htaccess的文件。注意,文件名前一定要有符号“.”,这样服务器才能知道该文件是隐藏文件,从而提高文件的安全性,避免错误操作。在.htaccess文件中需要加入以下三行文字:

Options Indexes FollowSymLinks Includes
AddType application/x-httpd-CGI .CGI
AddType text/x-server-parsed-html .shtml

  完成之后可以把.htaccess文件上传到服务端的相应目录,该文件对所有子目录有效。如果用户希望在目录级上禁止CGI或shell命令,可以在.htaccess文件中的Options选项行加入关键字IncludesNOEXEC。

3.使用.shtml还是.html?

  任何包含SSI的文件在下传到客户端之前,都必须经过服务器的解析过程。虽然从某种程度上这样会增加服务器的负载,但是除非用户的网站每天有几百万的访问用户,某则服务器的性能不会有明显的下降。不过,如果不是需要在每个页面都使用SSI的话,确实没有必要让服务器解析每一个页面。如果用户只希望在几个特殊页面中使用SSI,可以将文件的后缀名改为.shtml,这样服务器就可以只解析包含SSI的.shtml文件。另一方面,如果有多个页面使用了SSI,但是用户不希望使用.shtml的后缀名时,可以在.htaccess文件中使用以下命令行:

AddType text/x-server-parsed-html .html

SSI语法

SSI在使用时遵循以下格式:

$#@60;!--#directive parameter="value"--$#@62;

Among them, directive is the name of the instruction sent to the server, parameter is the operation object of the instruction, and value is the instruction processing result that the user hopes to get.
All SSI commands start with “$#@60;!--#”, and there cannot be any space between “$#@60;!—” and “#”, otherwise the server will process the SSI command When called ordinary file comment processing, no results will be displayed, and no error prompts will be generated. In addition, there cannot be spaces on both sides of "=" in the SSI command. The value on the right must be enclosed in double quotes, followed by spaces, and finally the closing tag "--$#@62;".

SSI commands include six categories of instructions and their respective parameters, as follows:

directives

parameters

Config

errmsg, timefmt, sizefmt

include

virtual, file

echo

var

fsize

file

flastmod

file

exec

cmd, cgi

directives

parameters Config errmsg, timefmt, sizefmt

include

virtual, file

echo

var

fsize

file

flastmod

file

exec

cmd, cgi

This news has a total of

2 pages, currently on page 1

1 2

We will introduce them one by one below.

1.Config command The Config command is mainly used to modify the default settings of SSI. Among them:

 Errmsg

:

Set the default error message. In order to return the user-set error message normally, the Errmsg parameter must be placed in front of other SSI commands in the HTML file. Otherwise, the client can only display the default error message instead of the custom message set by the user. $#@60;!--#config errmsg="Error! Please email webmaster@mydomain.com --$#@62;

Timefmt : Define the format used for date and time. The Timefmt parameter must be used before the echo command.

$#@60;!--#config timefmt="%A, %B %d, %Y"--$#@62;

The displayed result is: Wednesday, April 12, 2000 Perhaps the user is unfamiliar with the %A %B %d used in the above example. Below we will summarize some of the more commonly used date and time formats in SSI in table form.
Format
Description Instance %% % %a Abbreviation of seven days a week Thu http://www.bkjia.com/PHPjc/532651.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532651.htmlTechArticleHave you ever or are you wondering how to complete a website with thousands of pages in the shortest possible time? Worried about revisions? Then you can read the introduction of this article, maybe it can help you...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template