Relative and Virtual file Including in ASP

Efficient coding requires reusable common code which gets linked to more than one file. In ASP we will be using file include to add codes inside one external file to our required file. There can be many reasons to use file include inside our ASP scripts, some important requirements we will discuss here.

Menu or links area

Let us say we have a site with more pages ( say one hundred ) and we have added one more page to this site. The link of this section or page has to be connected to all hundred pages so we have to edit all the required pages were the link to this new page is to be added. A better solution is to use one menu.asp file and include the file inside every asp file like this

<!---#include file="menu.asp" --->

We only have to add our new page link inside the menu.asp file and it will appear all the pages which have included this menu.asp file.

Here the menu.asp file is located in the same directory as the main files calling this include file. If this is inside another directory say my_dir then it has to be included like this.

<!---#include file="my_dir/menu.asp" --->

Another requirement is to keep the database connecting string. Say your host has given you a set of login id , password and database detail ( known as connecting string ). We will be using database in many files so using the connecting string to connect to database. So rather than keeping this connecting string in each and every file we will keep in one connection.asp file and include this inside all required files. Now this will help us a great when we shift our site to a new host. As the new host give a new connection string we need to change this connection.asp file only. Another way it helps we are maintaining a copy of the site in our local computer with its own connecting string.

Including Virtual

We can include a file located from the root of the virtual directory. Say our connection string is kept at root of our virtual directory. Now from any level the same file can be included like this .

<!-- #include virtual = "/connection.asp" -->

Another point we have to keep in mind while keeping sensitive data like connecting string is file extension. If we keep the file name as and any one open the file in browser then all the code with database login details will be exposed. So we have to keep these data inside a file with .asp extension.

Include files are always included first before all other codes of main file gets executed. So we can't keep any condition like if else to dynamically include any file.

You can read how to include file in php here

Be the first to post comment on this article :

Post your comments , suggestion , error , requirements etc here .

We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2022 All rights reserved worldwide Privacy Policy Disclaimer