萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP 程式設計

ASP 入門:表單資料儲存至文字檔

情境

簡單示範如何接收表單 POST 資料,以計數器命名檔案,將每筆資料存為獨立文字檔。

前端表單

<FORM ACTION="saveinfo.asp" METHOD="post">
  <!-- 任意輸入欄位 -->
  <INPUT TYPE="submit" VALUE="Submit">
</FORM>

後端處理 (saveinfo.asp)

<%
Set fs = CreateObject("Scripting.FileSystemObject")
FolderPath = Server.MapPath("") & "/cgi-bin/messages143718"
Wcounter = FolderPath & "/counter.txt"

' 讀取/初始化計數器
If fs.FolderExists(FolderPath) Then
    Set a = fs.OpenTextFile(Wcounter)
    hits = CLng(a.ReadLine)
    hits = hits + 1
    a.Close
Else
    fs.CreateFolder(FolderPath)
    hits = 1
End If

' 更新計數器
Set a = fs.CreateTextFile(Wcounter, True)
a.WriteLine(hits)
a.Close
Set fs = Nothing

' 以計數器為檔名儲存表單資料
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(FolderPath & "\" & hits & ".txt")
For Each x In Request.Form
    a.WriteLine(x & ": " & Request.Form(x))
Next
a.Close
Set a = Nothing
Set fs = Nothing
%>

安全性與維護提醒

  • 直接寫入使用者輸入未經過滿,易遭注入惡意內容;實務上需做 HTML 編碼或參數化處理。
  • 大量檔案會導致目錄掃描緩慢,建議改用資料庫或依日期分資料夾。
  • 檔案權限需確保 IUSR/IIS_IUSRS 可寫入。
copyright © 萬盛學電腦網 all rights reserved