C and S Design.
Search Friendly Programming and Design



ASP Include Directive

The syntax of the include directive in ASP is identical to to the SSI directive and both variations can be used.

<!--#include file=...-->
and
<!--#include virtual=...-->
Where server side scripting differs from SSI is that the includes can be a code module of functions that are included into the page and can be called at any point where required. If the include file contains an block of executable code not defined as a function it will be executed at the point it is called and the output from the script will be included in the page.
With ASP includes the file calling the include must have a file extension of .asp. For the files getting included if they only contain HTML code or text the extension can be any that the server can parse as text.
if there is executable script code in the file there are two extension can be used as standard for the included file, .asp or .inc. It should be noted that although the server will treat a .inc file as executable code when used in an include directive, should the file be requested by a browser as here, .inc file the browser will render the file as text. Whereas if the .asp file is requested no browser output will occur because the server will parse the file first. The point being here is that if you are hardcoding database names, usernames and passwords for a SQL database connection in an include file DO NOT USE A .INC EXTENSION. This can (as shown) pose a security risk. The file used to demonstrate is a freely downloadable file from MS so nothing of value is in there.

The Include Directive

If the included file is in a subfolder or is in the same folder relative to the current document the #include file argument is used and if the included file is in a folder above the current document, the #include virtual argument is used. The file argument cannot be used to go up the folder tree by using <!--#include file="../filename"-->

for example using this folder structure

root---- www
        |----include
        |      |
        |      |---functions.asp
        |      |---footer.asp
        |      |---header.asp
        |      |---style.css
        |
         ---reports
              |---default.asp    
              |---content.asp    

To have default.asp include functions.asp, header.asp, content.asp and footer.asp the page code would be;

<!--#include virtual="/include/functions.asp"-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Reports Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="Reports page">
<meta name="keywords" content="keywords">
<link href="/include/style.css" rel="stylesheet" type="text/css">
</head>

<body>

<!--#include virtual="/include/header.asp"-->
<%breadcrumbs()%>
<!--#include file="content.asp"-->
<!--#include virtual="/include/footer.asp"-->

</body>
</html>

In this example a code module is included (functions.asp) and a function called from it (breadcrumbs()).

Valid HTML 4.01! Valid CSS! copyright © C and S Design 2004 - 2005
Website Design and SE Friendly Coding C and S Design