ASP 101 - Active Server Pages 101 - Web05
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

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

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
What is Adovbs.inc and Why Do I Need It?
An Overview of ASP.NET
Connections, Commands, And Procedures

QUICK TIP:
Leaving Scripts and Styles Inline
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


404 Error Report

By: Lewis Moten

Description

Used to catch 404 errors and report them to the webmasters email address. Also gives user a friendly message.

Notes

By default, this script will attempt to send the 404 error report to the webmaster of the pages domain that it is hosted on. You can change this by editing the address sent with the SendReport Function Call. If you want to set it back to the default setting, change this text to "webmaster@domain.com"

Simply place this page on your web site. Placing it in the following folder will make it accessible from the root folder of your web site.

Place in this folder: "C:\InetPub\wwwroot\404.asp" URL: "http://www.domain.com/404.asp"

If you attempt to view this page without passing a query string, email will not be sent.

Redirection Setup (IIS, SBS, Site Server, MCIS)

At the time of this scripts development, Personal Web Server does not support custom error pages.

Open Microsoft Management Console - Usually label as "Internet Service Manager"

Start\Programs\Windows NT 4.0 Option Pack\Microsoft Internet Information Server\Internet Service Manager

Under Console Root\Internet Information Server you should see a computer with the name next to it representing your computers Machine Name

Edit the properties of the web site that you would like to add the custom error page (Usually labeled as "Default Web Site") Click on the Custom Errors Tab Find the HTTP Error that says 404 and click the button "Edit Properties" Change the message type to URL Type in the URL to this page (If placed in the root directory it would be "/404.asp") Click "OK"

Now your page will be shown each time a 404 occurs.

SMTP Setup (For CDONTS)

The object CDONTS.NewMail was used because it is usually installed in a basic installation of IIS. Other COM Objects can be used to send mail, or you may even wish to log the errors to a database for advanced reporting.

CDONTS relies on the SMTP service that comes with IIS. You may need to go into Microsoft Management Console to Start the service, and allow asp pages to send email with this service.

Open Microsoft Management Console - Usually label as "Internet Service Manager"

Start\Programs\Windows NT 4.0 Option Pack\Microsoft Internet Information Server\Internet Service Manager

Under Console Root\Internet Information Server you should see a computer with the name next to it representing your computers Machine Name

If the SMTP Site is not showing up under the computer, you may need to add a snap-in

On the menu, click "Console" and find "Add/Remove Snap-in..."

Click the extensions tab Find the extension labeled "Mail - SMTP", click the checkbox, and press OK

A mail icon should show up labeled "Default SMTP Site", view its properties

Click the Operators tab The internet user account on the web server needs to be on here. This user account starts with "IUSR_" followed by your computers Machine Name. If my computer is named verioGuy, then the user "IUSR_verioGuy" Needs to be present.

Click on the add button if the user is not present If you can not find the "IUSR_" account with your machine name, then you may need to press search.

Make sure you are searching for users on your machine and not on the domain. If my Machine is verioGuy, then I need to search for "verioGuy\IUSR_verioGuy"

After this is finished, you can now have 404 errors emailed to you from the server.

If the email is successfully sent, the user will also be told that the error has been reported to the webmaster.

The Code

<%
' -----------------------------------------------------------------------------
' 404 Error Report
' -----------------------------------------------------------------------------
' Author: Lewis Moten
' Email: lewis@moten.com
' Web Site: http://www.lewismoten.com
' Created: February 16, 2000
' Description:
' Used to catch 404 errors and report them to the webmasters email address.
' Also gives user a friendly message.
' -----------------------------------------------------------------------------
%>
<HTML>
<HEAD>
	<TITLE>404 - Page Not Found</TITLE>
	<META NAME="ROBOTS" CONTENT="NOINDEX, FOLLOW">
</HEAD>
<BODY bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#0000ff" alink="#0000ff">
<FONT face="Helvitica,Arial,Sans-serif" size="2">
<B>The page could not be found on the server (404)</B><BR>
<BR>
<%
' If a user went directly to this page (without errors)
If Request.QueryString = "" Then
	' Tell the user that this page is ment for handling missing pages
	%>
	<P>
		This page is not ment to be viewed on its own.  It is
		here to capture and report missing web pages on this
		server.
	</P>
	<%
Else
	' Let the user know that the page is missing
	%>
	<P>
		It appears that you have stumbled upon a page that is not
		present on this web site.  It could have been moved, spelled
		incorrectly, or it may even be in our plans to expand the
		site and develop this page.
	</P>
	<P>
		<%
		' If we were able to send a report to the webmaster
		If SendReport("webmaster@domain.com") Then
			' Tell the user we sent a report
			%>
			A message has been sent to the webmaster about this missing page.
			<%
		End If
		%>
		We apologize for any inconvenience that this may have caused.
	</P>
	<%
End If
%>
</FONT>
</BODY>
</HTML>
<%
' -----------------------------------------------------------------------------
Function SendReport(ByRef asToAddress)
	Dim loMailer			' Mailing object
	Dim lsBadLink			' The link that was not found
	Dim lsBody				' body text to be sent in email
	Dim lsReferer			' URL that refered site to this page
	Dim lsServerName		' Server Name
	Dim lsToAddress			' Address to send mail to
	'-----
	' If the default GENARIC email address is provided
	If asToAddress = "webmaster@domain.com" Then
		' Create a generic email address to send the 404 error to
		' (webmaster@domain.com)
		lsServerName = Request.ServerVariables("SERVER_NAME")
		If LCase(Left(lsServerName, 4)) = "www." Then
			' Cut off the "www." prefix
			lsServerName = Mid(lsServerName, 5)
		End If
		lsToAddress = "webmaster@" & lsServerName
	Else
		' Use the email address provided
		lsToAddress = asToAddress
	End If	
	'-----
	' Store the URL that refered the page
	lsReferer = Request.ServerVariables("HTTP_REFERER")
	'-----
	' Acquire the URL that was not found
	lsBadLink = Request.QueryString
	' Remove the "404;" prefix
	lsBadLink = Replace(lsBadLink, "404;", "")
	'-----
	' Compose a report
	lsBody = "The following file was not found:" & vbCrLf & vbCrLf
	lsBody = lsBody & lsBadLink & vbCrLf & vbCrLf
	' If there was a referer
	If Not lsReferer = "" Then
		' Add the referer to the report
		lsBody = lsBody & "The following page refered the link:" & vbCrLf & vbCrLf
		lsBody = lsBody & lsReferer
	End If
	'-----
	' SEND THE MESSAGE
	'-----
	' Keep errors silent in case mailing object is not present,
	' or the smtp properties do not allow for sending email.
	On Error Resume Next
	' Create the mailing object
	' Collaboration Data Objects for NT Server (CDONTS)
	Set loMailer = Server.CreateObject("CDONTS.NewMail")
	' Set message properties
	loMailer.From = lsToAddress
	loMailer.To = lsToAddress
	loMailer.Importance = 2
	loMailer.Subject = "Reporting 404"
	loMailer.Body = lsBody
	' Send the message
	loMailer.Send
	' Release object from memory
	Set loMailer = Nothing
	' Determine if we have succeded in sending the report
	If Err Then
		SendReport = False
	Else
		SendReport = True
	End If
End Function
' -----------------------------------------------------------------------------
%>

The code along with a text copy of these instructions can be downloaded from here.

About the Author

Lewis Moten can be reached via email lewis@moten.com or his web site http://www.lewismoten.com. When he's not writing for us he works as an advanced asp developer for Verio Custom Web Design who provides clients around the world expert design and programming services to deploy top-notch web sites.


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers