If you are getting into web development, include files are going to become one of your best friends. In essence, an include file allows you to write an ASP script one time and then include it in multiple web pages without having to rewrite all the code on every page.
So when and how do you do this? One of the most basic examples is placing your Open Database Connection script within an include statement and then using a simple include statement to include your connection string in your web pages.
First, you create a folder named /includes within your root directory.
Next, you create a new file named /Connection.asp and save it within your new /includes folder.
All that is left is to input a simple one line include statement into each page where you need to have a database connection. Here is how the include statement looks:
The include statement cannot be placed inside of your <% %> ASP delimiters. If you need to place an include statement within your ASP code, you will need to close your ASP code, input the include statement, and reopen your ASP code like this:
<% ' ASP code here %> <!--#include virtual="/includes/connection.asp"--> <% ' ASP code here %>
OK, you’ve got that. Now what is the advantage of using an include file like this? Well, think about it. Let’s say your website is 100 pages and that 6 months from now something in your database connection changes. Rather than having to go through and edit your connection string in all 100 pages, you can simply edit it one time in your include file and that will automatically update it throughout your website.
There are actually two ways of including files in a web page. You can use the INCLUDE VIRTUAL or INCLUDE FILE method. My preference is to use INCLUDE VIRTUAL. This means that you can just enter the Virtual Path of your website to the file you want to include. In the example of the path is “/includes/connection.asp”.