|
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.
|