Server Side Includes (SSI) is a simple server-side scripting language used to embed small dynamic code snippets into HTML pages. Originally developed to make web pages more dynamic and easier to maintain, SSI can perform tasks like inserting headers and footers across multiple pages, displaying the current date and time, or even running shell commands on the server.
SSI is an efficient way to manage and update website content with minimal effort, especially on sites with repetitive content across multiple pages. In this chapter, we’ll dive deep into SSI, covering its purpose, syntax, practical applications, and advanced features.
Server-Side Includes is a lightweight server-side scripting language allowing HTML files to include dynamic content generated by the server. When an SSI command is embedded in an HTML file, the server processes it and includes the result in the HTML document before sending it to the client.
SSI is ideal for small tasks that require server processing, making it efficient for repetitive or frequently updated elements on a website.
SSI commands are typically written as HTML comments with specific syntax so that the web server processes them. Here’s the general format:
<!–#command parameter=”value” –>
command
: The SSI directive (such as include
, echo
, config
).parameter="value"
: The argument or setting for the directive.Note: SSI syntax should look like an HTML comment so that if a browser encounters an SSI command that wasn’t processed by the server, it won’t display it to users.
For SSI to work, the server must be configured to recognize .shtml
files or files with other designated extensions that contain SSI commands. Some web servers, like Apache, require enabling SSI in the server configuration or .htaccess
file.
.htaccess
file:
Options +Includes
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
After adding this, ensure that any file with the .shtml
extension is processed for SSI commands.
Here are some of the most frequently used SSI commands and their applications:
include
– Including External FilesThe include
command inserts the contents of another file into the current file, useful for headers, footers, or navigation menus.
<!–#include file=”header.html” –>
file
attribute specifies a relative path to the file that should be included. The content of header.html
will appear where this command is placed.Output: The HTML content of header.html
will display at the location of this command in the final HTML file.
echo
– Displaying VariablesThe echo
command is used to print the value of environmental variables such as the current date.
<!–#echo var=”DATE_LOCAL” –>
var="DATE_LOCAL"
displays the server’s current local date and time.Output: For example, Saturday, 21-Oct-2024 13:25:59 PDT
.
config
– Setting SSI Output OptionsThe config
command customizes the format for displayed information, such as the date format.
<!–#config timefmt=”%Y-%m-%d” –>
<!–#echo var=”DATE_LOCAL” –>
timefmt="%Y-%m-%d"
sets the date format to YYYY-MM-DD
.echo
command will now display the date in this format.Output: 2024-10-21
fsize
– Displaying File SizeThe fsize
command shows the size of a specified file, useful for indicating download sizes.
<!–#fsize file=”download.zip” –>
Output: 1.5 MB
(for example).
flastmod
– Displaying Last Modification DateThe flastmod
command shows the last modified date of a file, useful for showing when content was last updated.
<!–#flastmod file=”news.html” –>
Output: Tuesday, 20-Oct-2024 15:43:12 PDT
Let’s create a layout where all pages share the same header and footer.
My Website
© 2024 My Website
Home Page
Welcome to my website! This is the home page.
Explanation: The header.html
and footer.html
content will be included in the main.shtml
file. Changing the header or footer only requires editing a single file.
© 2024 My Website
Last updated:
Explanation: The echo
command displays the server’s current date and time each time the page is accessed.
SSI supports basic conditional statements using the if
, elif
, and else
directives.
Happy Sunday!
It's Saturday, the weekend is here!
Have a great day!
While SSI is a useful tool for simple server-side tasks, it has some limitations:
.shtml
files.Server-Side Includes (SSI) provides an easy way to embed dynamic content in HTML pages without requiring a complex scripting environment. It simplifies website maintenance, especially when managing common elements across multiple pages, like headers, footers, or navigation. Happy coding !❤️