Server Side Includes (SSI)

Homepage > ASP Tutorials > Server Side Includes (SSI)

Server Side Includes are a good way of recycling commonly used ASP code throughout your website and can save you a lot of time when you're making site-wide updates.

Let's say you include a header.asp at the top and a footer.asp at the bottom of every page on your site. These files contain all of the HTML that makes up the layout of your page -- your banner, navigation, meta tags etc. Now instead of having huge chunks of the same HTML repeated on every single page, it might look a bit more like this:

<!--#include file="template/header.asp"-->

<h1>My homepage</h1>
<p>Here is some page content....</p>

<!--#include file="template/footer.asp"-->

Your site grows and you need to add a new link on every page. Instead of having to update every page individually, now you only have to edit one file to make wholesale changes throughout your entire site.

File includes

Add this syntax to your page to include a file:

<!--#include file="template/header.asp"-->

A file include is relative to the folder your file is located. So what happens if you place this code on pages in different folders?

Page: www.yoursite.com/default.asp
The server looks for: www.yoursite.com/template/header.asp

Page: www.yoursite.com/widgets/default.asp
The server looks for: www.yoursite.com/widgets/template/header.asp

As you can see, the server is looking in different folders for the header file, so you would need a header.asp in two different folders. This defeats the object of using includes. If your file is in the "widgets" directory, you could modify the syntax to make the server move up a folder:

<!--#include file="../template/header.asp"-->

The advantage of this technique is that you can include files located outside the web root. Although I'm sure this has it's uses, I haven't found one yet, so I recommend you use virtual includes instead...

Virtual includes

You can place the following code on any page, in any folder of your website and the server will always look for www.yoursite.com/template/header.asp:

<!--#include virtual="/template/header.asp"-->

When you use virtual includes, the server always starts looking from the root folder of your website. So, no matter which directory you're in, you can always use the same syntax to access the same folder:

If use a text editor such as UltraEdit you might have something like a "Replace In Files" feature, which works just like a find and replace, but goes through all files in a directory. If you change the folder that you store your template files in, this makes it quick and painless to update every page to point to the correct include file.

Avoid using .inc extensions

Microsoft say "it is considered good programming practice to give included files an .inc extension to distinguish them from other types of files".

Ignore this and give your include files a .asp extension. If anyone discovers the location of your .inc files (quite possible through unforseen bugs and errors), they can download them and view all of the code inside, possibly exposing you to a number of security vulnerabilities. Try downloading my adovbs.inc file to see what I mean.

Use .asp extensions and your code will be safely hidden away from prying eyes.

Dynamic includes

So what happens if you want a different file included based on your user's preferences?

<%
  if Request.Cookies("Preferences")("WebsiteColor") = "Blue" then
    sFile = "header_blue.asp"
  else
    sFile = "header.asp"
  end if
%>
<!--#include virtual="/template/<%=sFile%>.asp"-->

If you try using anything like the code above, you'll get this error thrown back at you:

Error Type:
Active Server Pages, ASP 0126 (0x80004005)
The include file '/template/<%=sFile%>.asp' was not found.

This is because the server includes files BEFORE any ASP is executed and because of this it's looking for a file called "<%=sFile%>.asp". This file couldn't possibly exist since the < and > characters aren't allowed in filenames, so you end up with a file not found error.

But, there are times where you might want to include a different file under certain conditions, for example changing a page template based on the value of a cookie.

Click here for more on dynamic includes in ASP.

Rate this tutorial!

  • Currently 2.7/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Rated 2.7/5 stars (404 votes cast)

Make an enquiry

Privacy policy