GuidesInclude Files with ASP

Include Files with ASP

by Robert Adamczyk

First, a little about myself:

My name is Robert Adamczyk but I go by “Check”. I’ve been doing web
development since around 1994 and have been programming since around
1982. I’ve programmed in C, C++, IAS indirect command files, DOS
batch files, C shell and Korn shell scripts, Perl, Clipper,
PowerBuilder, FoxPro, GWBasic, Basic, TI Extended Basic, QBasic,
QuickBasic and Visual Basic. My current development efforts are
directed towards Visual Basic. I use VB6 to write COM objects which
are stored in Microsoft Transaction Server (MTS) and called from the
VBScript in my ASP pages. I am the Webmaster of three websites with
a combined average of over 20,000 hits a week.

Do you have certain things that you cut and paste into a lot of your ASP pages? A better way to accomplish this might be to put the repeated code into a function or subroutine and save it in an include file.

Now on to the subject at hand, Include Files with ASP
you have certain things that you cut and paste into a lot of your
ASP pages? A better way to accomplish this might be to put the
repeated code into a function or subroutine and save it in an
include file. This has a lot of advantages and only a few minor

The drawback to this is that the server has
to open the include file which takes longer than if the code had
been placed directly in the ASP file. Most of the time though, the
difference in time is so small that it’s unnoticable. You’ve got to
decide how many functions and subroutines to put into each include
file. Make sure to group your functions and subroutines in a manner
that makes sense. Don’t put functions and subroutines together that
won’t be used on the same page. Doing that will make the pages load
slower, because they have to read in and process the functions and
subroutines even if they aren’t used. On the other hand, don’t
separate them too much because you will waste too much time opening
a lot of include files.

The benifits of using include files
with functions and subroutines is that the actual code is located in
a single place. Lets say you use the cut and paste method instead of
include files and you have the same block of code in 15 different
files. You then find a bug in the code or find a faster way to
accomplish the same thing. You would have to open and modify all 15
files to incorporate the changes. With include files, you make the
change in one place and all 15 files use the updated code.

They can also be used to include an image or heading
information in each of your files. It is good practice to use width
and height attributes for image tags. Lets say you have a logo that
you want displayed at the top of each page. Most of the time you
would just put the
If you use an include file instead, you can put the
height and width tags in the include file and only modify one
location when it changes. Again, don’t over do these, since it is
another file that has to be opened for the page to be

Now that I’ve mentioned the virtues of using
include files, you’re probably wondering how to use them. They are
actually quite easy to implement. The first thing you need to do is
open an empty text file. You place whatever code you are going to
re-use in this file. Whatever you place in this file will be placed
in your page exactly where you put the include tag. Since include
files are actually implemented with an html tag and not through ASP
code, you will need to place any ASP code inside tags
within the include file. This means most of your include files will
begin with . If this include file will be
used for an image then you will just put the

 1 then
Welcome to our web site !

Login Now

SWYNK Homepage

Check's Homepage

Most of the time, your include
files should have an extension of .asp. This is so if someone
guesses the url of your include file, it will be processed before
being sent to the user. Normally this results in a blank page being
displayed instead of your code. Saving them with the extention of
.asp can make it a little confusing since you don’t have an easy way
of telling which are include files and which aren’t. This can be
solved by either naming them all inc_filename.asp or something
similar, or putting them in a subdirectory called include or inc.

Once you have decided where to place your include files and
what to call them the only thing left is to actually include them in
your ASP pages. To do this you use the html server side include tag.
It’s format is:

 For relative paths
 For absolute paths

This include tag must be placed outside any ASP code or it
won’t be processed. This means your page will look something like


Your Page Title

The rest of your page text here.

Assuming that the session.username variable has not been set, the above example page
(mypage.asp) would be displayed as follows:

Login Now

rest of your page text here.


this examply is kind of weak since there isn’t much code to it. You
can however use this example as a shell and put some useful code in
the function or subroutine.

If you are using this for an
image, your page might look something like this:

Your Page Title

The rest of your page text here.

In this case the line that says
would be replaced with the
code in the logo.asp file. Lets say the the logo.asp file contained
the following code:

The above example page would be
sent to the user as follows:

Your Page Title

The rest of your page text here.

All of the above examples use
the INCLUDE FILE tag to get the include files into the current ASP
page. It is also possible to use the INCLUDE VIRTUAL tag to
accomplish the same thing. The INCLUDE VIRTUAL tag however starts
the path at the root directory of the webserver instead of the
current directory. This same thing can be accomplished with the
INCLUDE FILE tag by simply starting the filename with / as in:

Instead of

In this example, the FILE tag requires one extra character.
However, what if your include files only apply to a specific
subdirectory? In this case the difference between the FILE tag and
the VIRTUAL tag might look something like this:

Instead of

Because of this, I personally prefer to use the INCLUDE FILE
tag vs the INCLUDE VIRTUAL tag.

Hopefully you can see from
this article the powerful potential of include files. If used
properly they can save you a lot of time while building and
maintaining your site.

(Note: This page uses six include files)

Latest Posts

Related Stories