Writing data to a text file

By ServerWatch Staff (Send Email)
Posted May 10, 1999


Ian Stallings
While writing a online signup form I decided to write some of the data to a text file for security reasons. I could've used a database, but the overhead isn't worth it and I only need notepad to open a text file. So, on to the code.

While writing a online signup form I decided to write some of the data to a text file for security reasons. I could've used a database, but the overhead isn't worth it and I only need notepad to open a text file. So, on to the code.

First we declare the textfile to be used and if it doesn't exist create it using this:

<%
LogFile = Server.Mappath("log.txt")
Set LogFileObj=Server.CreateObject("Scripting.FileSystemObject")


Set OutStream=LogFileObj.OpenTextFile(LogFile, 8, TRUE)

New_line = Request.Form("UserName") & Chr(13) & Chr(10)

OutStream.WriteLine(New_line)
TextFile.Close
%>

Now we can submit the info we want to collect from a form:

<html>
<head>
</head>
<body>
<form action="writetext.asp" method="post">
Username:<input type="text" size="20" name="UserName">
<input type="submit" name="submit">
</body>
</html>

For this script to work it must have write permission. The text file will be created in the same directory as the script. If you have questions or article ideas feel free to drop me a line.

Page 1 of 1


Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.