ASP Include Files
#include Directive
By using the #include directive, you can insert the contents of another ASP file into the current ASP file before the server executes it.
The #include directive is used to create functions, headers, footers, or other elements that need to be reused across multiple pages.
How to Use the #include Directive
Here is a file named "mypage.asp":
This is the "wisdom.inc" file:
This is the "time.inc" file:
If you view the source code in the browser, it will look like this:
Syntax for Including Files
To include a file in an ASP page, place the #include directive within comment tags:
Virtual Keyword
Use the keyword virtual to indicate a path starting from the virtual directory.
If a file named "header.inc" is located in the virtual directory /html, the following line will insert the contents of "header.inc":
File Keyword
Use the keyword file to indicate a relative path. The relative path starts from the directory containing the referencing file.
If you have a file in the html directory and "header.inc" is located in the html header, the following line will insert the contents of "header.inc" into your file:
Note that the path of the included file (headersheader.inc) is relative to the referencing file. If the file containing the #include statement is not in the html directory, the statement will not take effect.
Tips and Notes
In the above section, we used ".inc" as the file extension for included files. Note that if users try to browse the INC file directly, its contents will be displayed. If your included file contains confidential information or information you do not want users to see, it is better to use ".asp" as the extension. The source code in ASP files is compiled and not visible. Included files can also reference other files, and an ASP file can reference the same file multiple times.
Important: The included file is processed and inserted before the script executes. The following script will not execute because ASP will execute the #include directive before assigning the variable:
You cannot include file references between script delimiters. The following script will not execute:
However, this script can execute: