ASP 101 - Active Server Pages 101 - Web03
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





ASP Source Code:
<!-- This would almost certainly be in a separate file, but for
simplicity I'm including both the reading and writing portions
of the log in the same place. -->
<p><strong>Current Web Log Contents:</strong></p>
<table border="1" cellpadding="3" cellspacing="0" width="200">
	<tr>
		<td bgcolor="#BCBC96">
			<strong>Cool Stuff!!!</strong>
		</td>
	</tr>
	<!-- #include file="weblog.txt" -->
</table>
<br />
<br />
<p><strong>Add New Entry:</strong></p>
<%
'******************************************************************************
'******************************************************************************
' You'll want to manually format the include file to begin with.  Each entry
' on it's own line.  Each line terminated with a vbCrLf (carriage return/line
' feed) character.  Please don't leave any extras at the end of the file except
' the one at the end of the last line (aka. the one that puts you on the first
' blank line at the end of the file).  Which brings up an interesting point...
' an easy way to add more items to an include file is to add more blank lines
' to the end.  As each new piece of new content is added, a blank line will
' be removed instead of the old content.  So if a file currently has 5 entrys
' and you want ten, simply add 5 carriage returns to the end of the include
' file and save it.  Then use the script as normal.  You'll also want to check
' the permissions on this script and/or the include file.  If this script is
' secured via NT, then the user might already have rights to modify the
' include file.  Otherwise you'll need to modify the NTFS permissions to give
' the NTFS permissions on the include file to allow the anonymous internet
' user to write to it.
'******************************************************************************
'******************************************************************************
' Some functions to make my life easier!
'******************************************************************************
Sub AddNewEntryToFile(strNewEntry, strVirtualFileName)
	Dim strFileContents
	' Get existing file contents
	strFileContents = ReadTextFromFile(strVirtualFileName)
	' Pull oldest entry off the bottom
	' Assumes there is one vbCrLf at the end of each line and NO EXTRAS AT THE END OF THE FILE!!!
	strFileContents = Mid(strFileContents, 1, InStrRev(strFileContents, vbCrLf, (Len(strFileContents) - 2)))
	' Add new entry to the top
	strFileContents = strNewEntry & vbCrLf & strFileContents
	' Write full text including new entry back to file
	Call OverwriteFileWithText(strVirtualFileName, strFileContents)
End Sub
'******************************************************************************
Function ReadTextFromFile(strVirtualFileName)
	Dim strTemp
	Dim objFSO, objFile
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
	' Open the file as a text stream (1 = ForReading, True = Create)
	Set objFile = objFSO.OpenTextFile(Server.MapPath(strVirtualFileName), 1)
	' Read contents of the file
	strTemp = objFile.ReadAll
	objFile.Close
	Set objFile = Nothing
	Set objFSO = Nothing
	ReadTextFromFile = strTemp	
End Function
'******************************************************************************
Sub OverwriteFileWithText(strVirtualFileName, strFileContents)
	Dim objFSO, objFile
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
	' Overwrite existing file and get a text stream to new one
	Set objFile = objFSO.CreateTextFile(Server.MapPath(strVirtualFileName), True)
	' Write updated content
	objFile.Write strFileContents
	objFile.Close
	Set objFile = Nothing
	Set objFSO = Nothing
End Sub
'******************************************************************************
'******************************************************************************
' Runtime code...
'******************************************************************************
Dim strHeadline
Dim strURL
' Retrieve our form values	
strHeadline = Request.Form("headline")
strURL      = Request.Form("url")
If Request.Form("submit") = "Add Entry" And strHeadline <> "" And strURL <> "" Then
	Dim strNewEntry
	' Formatting is completely up to you... 
	strNewEntry = "<tr><td><a href=""" & strURL & """>" _
		& "<font color=""#0000FF"">" _
		& Server.HTMLEncode(strHeadline) _
		& "</font></a></td></tr>"
	Call AddNewEntryToFile(strNewEntry, "weblog.txt")
	%>
	<p>
	"<%= strHeadline %>" added to weblog.txt.
	</p>
	<!--
	Page doesn't update immediately because the text file gets
	included before the script runs! -->
	<p>
	<a href="<%= Request.ServerVariables("URL") %>">View updated page.</a>
	</p>	
	<%
Else
	%>
	<form action="<%= Request.ServerVariables("URL") %>" method="post">
	<table>
		<tr>
			<td><strong>Headline:</strong></td>
			<td><input type="text" name="headline" value="ASP 101" /></td>
		</tr>
		<tr>
			<td><strong>URL:</strong></td>
			<td><input type="text" name="url" value="http://www.asp101.com/" /></td>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit" name="submit" value="Add Entry"></td>
		</tr>
	</table>
	</form>
	<%
End If
%>

Back the the Sample Output

Back to the Sample Index


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