| << 10.4.1- The FileSystemObject Object and its Object Model | Chapter10 | 10.4.3- The Text Stream Object >> |
Server-Side Includes
Server-side includes (SSIs) are a very useful way of making your site easier to manage, and for providing extra information. The 'including' is done at the same time as the Active Server Pages interpreter gets to see the page, so generally include files should be placed at the head of your ASP file. This means that information placed in the file, when included in the web page at the top, can be used throughout the script code. If you wanted to, it is possible to use code to decide which SSI #include directives we want to put into action, if you place them within an If...Then construct. There are five basic types of SSI we can use. We can:
- Include text files in our pages, as they are loaded
- Retrieve the size and last modification date of a file
- Define how variables and error messages are displayed
- Insert the values of HTTP variables in the page sent back to the browser
- Execute other programs or scripts, such as CGI and ISAPI applications
Only the first of these is directly applicable to Active Server Pages. SSIs are normally used in a separate file, which can be referenced and loaded from an ASP file.
Including Text Files in a Page with #include
One of the most useful techniques with SSI is to insert pre-built blocks of text into a page. As an example, we created a function for our calendar page that calculated the last day of any month. We can save this as a text file called, say, GetLastDay.txt. Then, anytime that we want to use the function, we just add an include statement to the page and call the function:
<!-- #include file="GetLastDay.txt" -->
...
intLastDayAugust = GetLastDay(datAugust) 'call our included function
...
Note that an SSI is a file, and as such it's common for the filename to have an extension such as .asp, .txt or .inc. Beware of the security implications of choosing your file extension. For example, if your SSI has an .inc extension, and a user manages to browse to it by typing a URL into their browser, then the contents of the SSI will not be parsed by the ASP engine. This means that the user will get to see the code that you wrote in your SSI. For this reason, it's always safer to use the .asp extension for your SSIs.
One thing to watch out for is if you want to include script from another file, this file must contain complete script sections. In other words, it has to have opening and closing <SCRIPT> or <%...%> tags – we can't place part of the code section in an included file, and the rest in the main page. Theoretically, we could include half of, say, an If...Then construct in the file, and the rest in the main page, as long as each part was enclosed in <%...%> tags. This isn't likely to produce code that is easy to read or debug later, though!
Of course, the text we include doesn't have to be VBScript or JScript code. We can quite easily use it to include HTML or just plain text. If your site uses pages with standard footers for your copyright notice, or a standard <STYLE> tag to set up the text and page styles, these can equally well be stored as a separate file, and referenced with a #include statement.
Virtual and Physical File Addresses
The #include directive allows us to specify a file using either its physical or virtual path. For example, the file MyFile.txt could be in the directory TextFiles, contained underneath your current folder. If this directory also had an alias (virtual path) of /Texts set up, we could then reference it using either method:
<!-- #include file="TextFiles\MyFile.txt" --> 'physical path
<!-- #include virtual="/Texts/MyFile.txt" --> 'virtual path
We can also, as you've already seen, use relative paths. If the file is in the same folder, we just use the file name. If it's in the Projects subdirectory, we can use:
<!-- #include file="Projects\MyFile.txt" --> 'physical path
In this example, we will actually be building an SSI that can be added to any Active Server Page.
Try It Out – Enhanced File Info Display
The SSI in this example will display information about the current file to the client.
1. Use NotePad to create the FileDetails.asp file with the following source code.
<%
Dim strFDPathInfo, strFDPhysicalPath
strFDPathInfo = Request.ServerVariables("PATH_INFO")
strFDPhysicalPath = Server.MapPath (strFDPathInfo)
Dim objFDFSO, objFDFile
Set objFDFSO = CreateObject("Scripting.FileSystemObject")
Set objFDFile = objFDFSO.GetFile(strFDPhysicalPath)
%>
<P>
<HR>
<DIV STYLE="font-size:11; font-family: Verdana; ">
File Name: <B><%= objFDFile.Name %></B><BR>
Server Path: <B><%= strFDPathInfo %></B><BR>
Physical Path: <B><%= objFDFile.Path %></B><BR>
File Size: <B><%= objFDFile.size %> bytes</B><BR>
Date Created: <B><%= objFDFile.DateCreated %></B><BR>
Date Last Modified: <B><%= objFDFile.DateLastModified %></B><BR>
Date Last Accessed: <B><%= objFDFile.DateLastAccessed %></B><BR>
</DIV>
2. Save the file in your BegASPFiles folder.
3. This example is of a sever-side include file. This means that we will need to add it to an existing ASP file. For this example, let's pick the InteractiveDirectory.asp file from the previous example. Open this file in your favorite editor and add this entry near the bottom.
<TD align=right>
<FONT FACE="Tahoma" SIZE="2" COLOR="DarkGreen">
<%= objFileItem.size %></FONT>
</TD>
<TD align=right>
<FONT FACE="Tahoma" SIZE="2" COLOR="DarkGreen">
<%= objFileItem.DateLastModified %></FONT>
</TD>
</TR>
<%
Next
%>
</TABLE>
<!-- #include file="FileDetails.asp" -->
</BODY>
</HTML>
4. View the page in your web browser.
|
|
How It Works
This server-side include file has two primary functions. First, it needs to obtain a File object for the file that it is included in. Then, using that File object, we can display the information about the file itself.
If we were to just load this file directly from the server, then it would be displayed as text rather than being passed through the ASP engine. This would not be what we are looking for. The nice part about making it a server-side include file is that even though it is a separate file, when the server includes it into the other file, it acts as if it were part of that file. This means that when we get the File object that corresponds to the current file, we get the file that we are really interested in.
In the previous examples, we have shown how to get a File object that corresponds to the current file. In those examples, we went a step further and used that File object's ParentFolder property to get a reference to the current folder. In this server-side include file, we will stop once we get the reference to the File object.
One change that we have made from the earlier examples is that we are going to use different variable names. In the earlier examples, the variable names were both easy to read and corresponded closely to the data the variable contained. In a server-side include file, you need to be careful about the naming of any variables. Since this file is actually treated as part of the file that it is included in, you need to ensure that the variable names do not conflict. In our example, since we are leveraging code from the InteractiveDirectory.asp example, our variables would have been named
- strPathInfo
- strPhysicalPath
- objFSO
- objFile
If this server-side include file is then included into the InteractiveDirectory.asp file itself, then there would be two statements that each declared a variable with each of these names. This would cause a script error when the page was processed since these variables already exist within InteractiveDirectory.asp. To get around this, we have changed the names of the variables in the hope that they will be unique. Since this server-side include file provides additional file data, we have added a FD to each variable name. This results in variables named
- strFDPathInfo
- strFDPhysicalPath
- objFDFSO
- objFDFile
With our File object referencing the file we are interested in, we can now turn to the display of the information. The information will be visually separated from the rest of the page with a horizontal line generated by the <HR> tag. To set the formatting for all of the information, we have created a <DIV> section and set the text format properties for that container element. All of the information that is displayed is retrieved from a property of the File object except one. Since the FileSystemObject objects deal with files in their physical space, the File object has no information about the virtual path to the file as seen by the web server. Since we want to display that information on the client, we will need to retrieve that information from the HTTP Server Variables that are passed with each request to the server. The strFDPathInfo variable will contain the virtual path that points to the file we are currently viewing.
Up until now, we have been using the FileSystemObject objects to access information about the properties of folders and files in the physical file system. They also provide one other function with respect to files. With a valid File object, you can open the file itself as a text file and deal with the data contained inside it. To do this, you will interact with the file's data using the TextStream object.
| << 10.4.1- The FileSystemObject Object and its Object Model | Chapter10 | 10.4.3- The Text Stream Object >> |

RSS


