“Includes” are the most basic leap from amateur Web designer to Web professional. The reason is simple: Web professionals create and manage large, expensive websites, and “includes” make managing these large sites manageable (get it?). If running large websites makes you nervous, don’t leave this page just yet. Even on small projects, server-side includes make life a whole lot easier.
An ASP include, or server-side include, is a plain HTML, HTML/ASP, or ASP-only file that sits outside of a parent Web page. The power of a simple include file is tremendous—it allows you to re-use content that will appear on multiple pages throughout your site.
Always. Unless there’s a reason not to, of course. Any piece of content that will appear on multiple pages should be an include, especially on larger sites. As you become familiar with them, you will get creative in their usage. My favorite instances are site navigation, copyright info, footer links, and “What’s New” sections, to name a few.
You should use a small amount of discretion, however, when deciding when and where to use them. If the content is not reused on multiple pages or you don’t have a slick dynamic script that requires them, you don’t need to use includes. Include files put a minor amount of extra strain on the server. When using them correctly, the benefits far outweigh any performance issues. If you go crazy and start using them for every small bit of code, though, you may notice a difference in your site’s load time. Just something to keep in mind.
Let’s assume you have a 100 page website. On the bottom of all 100 pages you have secondary navigation, copyright information, and a link to your privacy policy. This situation literally BEGS for an include to be used.
Here’s an example of a basic ASP file (we’ll call it default.asp):
<html>
<head>
<title>My ASP Home Page</title>
</head>
<body>
Some text goes here
</body>
</html>
Here’s our ASP include file to include the footer links and copyright information (we’ll call it inc_footer.asp):
<p>
Copyright 2004-2005 mysite.com. All rights reserved.<br/>
<a href="default.asp">Home</a> | <br/>
<a href="about-us.asp">About Us</a> | <br/>
<a href="services.asp">Services</a> | <br/>
<a href="contact.asp">Contact</a> | <br/>
<a href="privacy-policy.asp">Privacy Policy</a> <br/>
</p>
To include our copyright and footer links in default.asp, we only need to add one line of code:
<!--#include file="inc_footer.asp"-->
default.asp now looks like:
<html>
<head>
<title>My ASP Home Page</title>
</head>
<body>
<p>Some text goes here</p>
<!--#include file="inc_footer.asp"-->
</body>
</html>
That’s it! For every other page in the site, you only need to add one line of code to insert the footer content. A new year rolls around? No problem. Open up inc_footer.asp, change the year, upload it, and the update is made across every page in your site.
Technically, you can name your ASP include file whatever you want, as long as it ends in ASP or INC. I recommend giving your include files a prefix of "inc-" (as in inc-filename.asp) or "inc_" (as in inc_filename.asp). You might even consider placing them in their own folder. I find that this helps in managing files, especially when working on a bigger site.
Make it a point to give your includes an .ASP extension only. The reason is this– if someone snooping around in your site guesses the correct filename for your include file and the extension ends in .inc, the full source code will be displayed in the browser. This is a huge security flaw, especially when you move into more complex programming (can reveal database connection string, passwords, etc.). If the snooper guesses correctly but your include ends in .ASP, the file will be processed first (the server recognizes it as a server-side programming file), and the person will only see the OUTPUT of the code, not the source code itself. Crisis averted.
You must be logged in to post a comment.





August 16th, 2006 at 5:22 am
Your article was very informative. I’m just starting to study asp classic this info that you wrote really helps a lot. thanks.