asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search










Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

FileExists Web Tool
By Steven Livingstone
Rating: 3.3 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    Building Web sites is becoming a complex business. It’s even more so when a large site contains many individual webs all controlled by the same Web administrator. One major problem is missing pages, which can occur after an author points to a page, makes changes in some places, but misses a few.

    Although there are tools such as Microsoft’s Site Server that can analyze links, it would be helpful to have a catchall tool to track missing pages before users get the dreaded HTTP error and realize the link is not available.

    This article describes an ASP-based tool that can catch missing links during navigation around a Web site and email the information to a site administrator. It directs all links toward the file redirect.asp with the URL of the page you are linking to appended as the QueryString parameter URL.

    The Links Page

    The script below shows the page default.asp, which represents an array of possible links you would expect on a Web site. It includes relative/virtual links and the full address to a local file and full address to a remote page. Note the use of the Server_Name attribute of the ServerVariable collection to allow the page to be move across servers using full URL links.

    
    
    <%@ Language=VBScript %>
    <HTML>
    <HEAD>
    <META name="VI60_DefaultClientScript" Content="VBScript">
    
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    </HEAD>
    <BODY bgcolor=lightgrey>
    Hi. Welcome to <b>NTA's </b> home page. Click on a link to tour my site.
    <P>
    <a href="redirect.asp?URL=http://<%=Request.ServerVariables("server_name")%>/FileExists/hello.htm">
    Local that exists</a>
    </P>
    <P>
    <a href="redirect.asp?URL=http://<%=Request.ServerVariables("server_name")%>/FileExists/default.htm">
    Local which does not exist
    </a>
    </P>
    <P>
    <a href="redirect.asp?URL=http://12.345.6.789/default.htm">
    Remote Link
    </a>
    </P>
    <P>
    <a href="mailto:ntw_uk@nta.net">Send an Email to me</a>
    </BODY>
    </HTML>
    
    
    

    The Redirection Page

    The crux of the tool is the redirect.asp page, which does all of the work in determining the existence of a file and directing the user appropriately and if necessary, emailing an administrator. We shall now walk through the main points of this code.

    
    
    <%@ Language=VBScript%>
    
    
    
    As we implement redirects, we should set the buffer property to TRUE, otherwise clients coming through a proxy server shall get a notification page informing them that they are being redirected.
    
    
    <% Response.Buffer=TRUE%>
    ‘redirect.asp
    <SCRIPT LANGUAGE=VBScript RUNAT=Server>
    
    
    
    The GetVirtual function determines whether the URL to which the client is being redirected is a local or remote full URL (with http:// etc.) or is a relative/virtual link. To determine if it is a full URL, GetVirtual checks for “http://” in the string, and if this is true, it follows up by checking whether it is a request for a local page. If the request is for a local file, then it splits the URL into an array based on the separator “/” and sets the vPath variable to the fourth array member (the “/” character is appended to make it derive from the root).

    We can understand this if we look at the following:

    
    
    http://12.345.67.89/myRoot/myDir/myfile.asp
    
    
    
    A split into array values would give

    tempPath(0) = “http:”
    tempPath(1) = “”
    tempPath(2) = “12.345.67.89”
    tempPath(3) = “myRoot/myDir/myFile.asp”

    We then check that no error has occurred and return the new local path. Note that any remote path shall be automatically redirected.

    
    
    FUNCTION GetVirtual(URL)
    	on error resume next
    	Dim vPath
    	dim tempPath
    	
    	if InStr(1,URL,"http://") then
    		if InStr(1,URL,Request.ServerVariables("LOCAL_ADDR")) then
    			'complete path
    			tempPath=Split(URL,"/",4)
    			vPath="/" & tempPath(3)
    		else
    			Response.Redirect(URL)
    		end if
    	else
    		'virtual path, leave as is
    		vPath=URL
    	end if
    
    	'return vPath
    	if err = 0 then 
    		GetVirtual = vPath
    	else
    		Response.Write("An error occurred")
    		GetVirtual = " "
    	end if
    End Function
    
    
    
    The FileExists function determines whether the local file (virtual or relative) that has been passed physically exists. We initially use the MapPath method, which returns a physical path for the file passed (e.g., c:\InetPub\myRoot\MyDir\myFile.asp). We then check if a file actually exists in this location by calling OpenTextFile, which causes an error if the file does not exist (there are many ways to check for this using the FileSystemObject Object). We close the function by returning the appropriate TRUE/FALSE and eliminating the object references we created.
    
    
    FUNCTION FileExists (ByVal FileName)
    	on error resume next
    	FileName = Server.MapPath (FileName)
    	Set objFile = Server.CreateObject("Scripting.FileSystemObject")
    	Set CheckFile = objFile.OpenTextFile (FileName, 1, FALSE, FALSE)
    
    	if err = 0 then 
    		FileExists = TRUE
    	  else
    		FileExists = FALSE
    	end if
    	
    	Set objFile=Nothing
    	Set CheckFile=Nothing
    end Function
    
    
    
    Another feature of this system is the ability to tell the site administrator that there was a problem locating a local file. SendEmail is a basic function that can do this. We simply use the NewMail features of Collaboration Data Objects (CDO) and set the appropriate properties. This inserts the details of the missing page and where the link to the page came from.
    
    
    Function SendEmail()
    		'send an email to the administrator
    		Set objMail = Server.CreateObject("CDONTS.NewMail")
    
    		objMail.From = "webInfo@nta.net"
    		objMail.To = "livinsb@rbos.co.uk"
    		objMail.Subject = "Missing Page"			
    		objMail.Body = "Please check the link to " & URL & " on the page " & Request.ServerVariables("HTTP_REFERER") & "."
    		objMail.Send
    		Set objMail=Nothing
    End Function
    </SCRIPT>
    
    
    
    The script below demonstrates the calling procedure of these functions. First, we get the address that the user is being redirected to (this can be via GET or POST). We pass this file to GetVirtual, and if FileExists is valid, the user is sent to the appropriate page. Otherwise email is sent to the administrator, and the user is forced to an error page, info.asp, with the referring page appending to the QueryString, as described below.
    
    
    <%	Dim URL
    	URL=Request("URL")
    	If FileExists(GetVirtual(URL)) Then	
    		Response.Redirect(URL)
    	Else
    		SendEmail()	'let the admin know that a link is missing
    		Response.Redirect("info.asp?Origin=" & Request.ServerVariables("HTTP_REFERER"))
    	end if
    %>
    
    <html>
    <head>
    </head>
    <body>
    An error has occurred.
    <html>
    
    
    

    Coping with Errors

    Rather than a simple error message, I prefer to be more graceful about letting the user know an error occurred. Whenever an error occurs, the user is sent to info.asp, which is actually a frame set with error.htm (see below) at the top of the frame and the original page, which contained the link, on the bottom.

    
    
    <HTML>
    <!—info.asp-->
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Developer Studio">
    <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
    <TITLE>Virtual ClassRoom Login Frameset</TITLE>
    
    <FRAMESET Rows="10%,*" FRAMEBORDER="No">
    	<FRAME id=error Src="Error.htm" Name="frmError" no resize>
    	<FRAME id=main Src="<%=Request.QueryString("Origin")%>" Name="frmMain" no resize>
    </FRAMESET>
    
    </HEAD>
    </HTML>
    
    
    
    Error.htm simply tells clients there was a problem accessing the page and an OK button allows clients to confirm they understand why they could not access the site. After clicking on this OK button, the main frame becomes the top frame, and the error message disappears.
    
    
    <HTML>
    <HEAD>
    <!-error.htm-->
    <META name="VI60_DefaultClientScript" Content="VBScript">
    
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE></TITLE>
    </HEAD>
    <BODY>
    
    <h4 style="background:lightblue">
    The file requested does not exist or is on another Web site.
    An administrator has been informed.       <
    INPUT type=button name=ok value="ok"></h4>
    
    <script language=vbscript>
    Sub ok_OnClick()
    'Make the main page the _top
    top.location.href=Parent.main.location.href
    End Sub
    </script>
    </BODY>
    </HTML>
    
    
    

    About the Author

    Steven Livingstone is Director of Networking Technical Associates in Glasgow, Scotland and President of the Association of Internet Professionls, Scotland, UK (http://www.citix.com/aip). Networking Technical Associates develops Enterprise Weblications and Enterprise Electronic Commerce solutions and they are also working on some future commerce advances. Steven is also currently working on a Site Server 3.0 book. Networking Technical Associate web page can be found at (http://www.citix.com) and Steven can be contacted at ceo@citix.com.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Supporting Products/Tools
    CustomError 2.0 for IIS
    When errors occur on a Web site, they should be handled in a way that helps the user to get back on track. Unfortunately, setting up customized error pages in IIS usually requires something many Web developers lack -- access to and familiarity with the Web server's administrative interface. With CustomError for IIS, developers can add error pages, coded by hand or created in their favorite editor, by simply uploading them to a designated directory. No administrator intervention is required.
    [Top]
    Other Articles
    Sep 22, 2004 - Unit Test - Testing with NUnit Framework
    Kamran Qamar introduces unit testing with NUnit and offers some best practices, tips, and tricks.
    [Read This Article]  [Top]
    Aug 10, 2004 - Implementing and Promoting Daily Builds
    Automatic daily builds is a well known software engineering best practice. This article introduces a strategy for implementing and promoting daily builds and offers tips and tricks for preventing and fixing breaks.
    [Read This Article]  [Top]
    Jun 21, 2004 - Using Open Source .NET Tools for Sophisticated Builds
    Building an application can be more than pressing F5. With an increasing number of quality packages being released, developers for the .NET platform now have options to create a very sophisticated build process. Aaron Junod describes a sample build environment and shows how a number of tools can work together to make reliable, predictable, and value-added builds.
    [Read This Article]  [Top]
    Jun 18, 2003 - Online Database Functions Testing Tool
    This short article provides source code for a classic ASP online database functions testing application and shows how to configure and use the tool for either SQL Server or Oracle.
    [Read This Article]  [Top]
    Jan 2, 2003 - Web Application Error Handling in ASP.NET
    One of many improvements ASP.NET brings to the development table is in error handling. Adam Tuliper whips up a simple ASP.NET solution for handling those pesky and unexpected post-production errors.
    [Read This Article]  [Top]
    Sep 10, 2002 - Tracing in .NET and Implementing Your Own Trace Listeners
    Mansoor Ahmed Siddiqui explains debugging and tracing and shows how to create custom trace listeners to help ensure hassle-free development.
    [Read This Article]  [Top]
    Sep 5, 2001 - Firing Events in a Shared Hosting Environment
    Firing events on a Web server is an easy task. However most of the easy solutions require you to have your own dedicated IIS or SQL Server on the Internet to play with, a privilege not shared by many. In this article, Matthew Muller shows you how to get the same functionality in a shared hosting environment.
    [Read This Article]  [Top]
    May 25, 2001 - Avoiding a Type Mismatch Error When Using ByRef with ASP and COM
    Unlike programming inside a complete VB system, when using ByRef with ASP and COM, a complication arises because ASP's VBScript is not typed, but the component's VB is typed. This article will briefly explain how ByRef can be used with ASP and COM.
    [Read This Article]  [Top]
    Apr 18, 2001 - Error Reporting - IIS 5.0
    The script in Mark Newlands' article this week handles how errors are displayed and logged. It can capture all values in use at the time (e.g. form, querystring, session,and application level) and records them if you set a Boolean value to do so - displays custom HTML if required. Sends email, logs to database, and/or text file.
    [Read This Article]  [Top]
    Mar 12, 2001 - Transact-SQL Improves Database Error-Handling
    Transact-SQL provides developers with several database error-handling methods. Use these functions to efficiently handle database errors and add an extra level of data validation. This article discusses the @@ERROR, SP_ADDMESSAGE, and RAISERROR functions and provides examples on how to implement them.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers