Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Understanding about Server Side Includes (SSI)

In this article we will talk about Server Side Include(SSI) and its implementation. Server Side Includes is an important aspect used in web application development.

Introduction to Server Side Includes (SSI)

SSI or Server Side Includes is placed as page directives which are placed in HTML pages. SSI allows us to add content which is dynamically generated to an existing HTML page without loading the entire page. SSI is a good approach when it is required to add small set of information but is said not to be a good approach when it is required to add large set of information.

Using SSI with apache http server:

To use SSI with apache http server we should have the following module installed and enabled:

  • mod_include

In addition to this we should have the following directive in the httpd.conf or in the .htaccess file:

  • Options + Includes

The above directive tells apache that we intend to permit files to be parsed for SSI. IT should be noted here that most of the configurations contain multiple option directives which can override each other. We should be careful while applying the options to the specific directory in order to assure that it gets evaluated.

We should tell apache which file is to be parsed. There are two ways to do this:

We can tell apache to parse any file having a certain extension e.g. .shtml using the following directives:

AddType text/html .shtml
AddOutputFilter INCLUDES .shtml

This approach has a disadvantage:

If we need to add SSI directive to an existing page, we need to change the name of the existing page and all the links to that page so that the new extension given to this page: .shtml gets reflected everywhere.

Another approach is to use the directive xBitHack as under:

  • xBitHack on

xBitHack tells apache to parse the files if they have the execute permission set properly. So rather than changing the file name we need to change file permission as under:

	chmod +x pagename.html

It is not a good approach parse all the html files for SSI as it requires Apache to read every single file which is sent to the client. If we do this, the application will have a degraded performance. So the better approach is to use the xBitHack. On windows based server we cannot set the file's permission using chmod.

Basic SSI Directive:

The SSI directive uses the following syntax:

<!--#element attribute=value attribute=value ... -->

The syntax is pretty simple and looks like an html comment. So the browser ignores it if the SSI is not correctly enabled. If the SSI is enabled appropriately the directive is replaced with it results.

Here are some examples of SSI implementation:

Displaying today's date:

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

The echo command displays the value of a variable. There are many standard variables which include the whole set of environment variables which are available to the CGI programs. We can also define our own variables with the set element. If we need to change the format of the date we use the config element with the timefmt attribute to get the format of our choice as under:

<!--#config timefmt="%A %B %d, %Y" -->
Today is <!--#echo var="DATE_LOCAL" -->

Display the last modified date of a file:

	This document was last modified on <!--#flastmod file="index.html" -->

The element 'flastmod' is also subject to ‘timefmt’ configuration i.e. we can change the format of the date as per our choice.

Include a common header or footer:

If we have a website which has some common information at the bottom or on the top of every page, it is a real pain if we are required to make some change in this common information. Even if we are not required to change the common information, it is very important that we maintain uniformity across all the pages on the website. Using the #include file directive we can achieve this.

<!--#include virtual="/header.html" -->
<!-- HTML Code for the whole Page -->
..............
..............
<!--#include virtual="/footer.html" -->

In addition to be able to change the time format, we can also change the config of following two things:

The Error message: When something goes wrong with the SSI directive, we get the standard error message as under :

  • "an error occurred while processing this directive" .

If we need to change this standard message and use our customized error message we can do this by using the errmsg attribute of the config element as under:

" <!--#config errmsg="There is a problem in processing your request. Please contact the site administrator and lodge a complaint about this. --> " .

The File Size: We can change the format in which file sizes are returned with the sizefmt attribute of the config tag . We can specify bytes for a full count in bytes, or abbrev for an abbreviated number in Kb or Mb, as per our need.

Executing Commands:

In order to execute a shell command or a CGI script we should use the exec command. If we use the 'Includes NOEXEC' option, the exec command gets deactivated completely and we won't be able to run any shell command. The exec command accepts the following parameters:

cgi - This value specifies a URL relative to a CGI script which we want to execute on a certain page of the website. The document referenced by the path here is invoked as a CGI script. The directory containing the script must be enabled to execute CGI scripts with ScriptAlias or ExecCGIOption.

<!--#exec cgi="/var/applicationHome/cgi-bin/example.cgi" -->

cmd - This parameter specifies a command which is executed from the browser. e.g if we want to see the files or folders within a certain directory, we use the following:

<pre>
	<!--#exec cmd="ls" -->
</pre>

Or, on Windows we use the following:

<pre>
	<!--#exec cmd="dir" -->
</pre>

Some Advanced features:

In addition to displaying some content SSI provides us the option to set variables. Using these variables we can do comparisons and put conditions as under:

Setting variables: Using the set directive, we can set variables to be used later. The syntax to set variables is as under:

	<!--#set var="name" value="John" -->

Here we set a variable to a hardcoded String. In addition to this we can initialize a variable to the environment variable as under:

	<!--#set var="lastModified" value="$LAST_MODIFIED" --> 

The '$' symbol indicates that we want to use the value of an environment variable. However if we are required to print the '$' symbol we need escape the '$' symbol with a backslash as under:

	<!--#set var="costOfBook" value="$\99.00" --> 

Finally if we are required to concatenate two environment vales putting some character in between as a separator we can use this as:

	<!--#set var="date" value="${DATE_LOCAL}_${DATE_GMT}" -->

The above code displays both the local date time and the GMT date time with a separator '-' in between them.

Conditional Expression: Once the variables are defined and set, we should be able to compare their values. This feature of SSI allows it to be called a small programming language. The mod_include provides anif, elif, else, endif structure for putting conditional statements. This feature allows us to generate few logical pages from one page based on some condition which can be checked at runtime. The structure of putting conditional expression is as under:

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

It is important to note that we can’t use SSI on pages generated by a CGI script - until and unless the script can generate pages with .shtml extension.

Conclusion

To conclude this document let us summarize what we have discussed so far in the following bullets:

  • Server Side Includes or SSIs are placed as page directives in the HTML pages.
  • SSI allows us to add content on web pages which are dynamically generated.
  • SSI is best used with apache http server.
  • SSI can be used to have the following useful outputs:
    • Display the date and time dynamically on every page.
    • Display the contents of a folder on the browser.
    • Setting variables dynamically and display them on the browser.
    • SSI is also used a small programming language to put some conditional structuring.
  • SSI can be used with CGI only if the generated pages have the extension - .shtml.


Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login