#include
The #include Directive in ASP
The #include directive allows you to insert the contents of one ASP file into another before the server processes the page.
This directive is commonly used to reuse code such as functions, headers, footers, menus, or shared components across multiple pages.
Using the #include Directive
Consider the following ASP file named mypage.asp:
Words of Wisdom:
The time is:
Contents of wisdom.inc:
"One should never increase, beyond what is necessary, the number of entities required to explain anything."
Contents of time.inc:
<% Response.Write(Time) %>
When the page is processed by the server, the included files are merged into the page.
The browser finally receives plain HTML like this:
Words of Wisdom:
"One should never increase, beyond what is necessary, the number of entities required to explain anything."
The time is:
11:33:42 AM
Syntax for Including Files
To include a file in an ASP page, the directive must be written inside HTML comment tags:
or
Using the virtual Keyword
The virtual keyword is used when the file path starts from a virtual directory.
Example:
If header.inc is located in the virtual directory /html, use:
Using the file Keyword
The file keyword is used with a relative path, which is based on the directory of the current file.
Example:
If your current file is inside the html folder and header.inc is located in html\headers, use:
⚠️ The path is always relative to the file that contains the #include directive. If the including file is located elsewhere, the include will fail.
Tips and Important Notes
Included files often use the .inc extension
If a user accesses an .inc file directly, its contents may be visible
For sensitive or confidential code, it is safer to use an .asp extension
ASP source code is not visible in the browser after execution
An included file can include other files
The same file can be included multiple times in a single ASP page
Important Rule About Execution Order
Included files are inserted before ASP scripts are executed.
Because of this, the following code will not work:
<% fname = "header.inc" %>
The #include directive is processed before the variable fname is assigned a value.
Script Delimiters and Include Files
You cannot open or close ASP script blocks inside an included file.
This will NOT work:
<% For i = 1 To n Next %>
This WILL work:
<% For i = 1 To n %> <% Next %>