資料庫: regdb.mdb (放置於網站目錄下不可直接下載的資料夾,如 /data/)
資料表: Users
欄位:
- U_ID: AutoNumber (主鍵)
- U_Username: Text (50), Unique Index
- U_Password: Text (255) -- 實務存雜湊值
- U_Email: Text (100)
- U_RegDate: DateTime, Default Now()
- U_LastLogin: DateTime, Nullable
- U_Status: Integer (0=停用, 1=啟用, 2=待驗證)
<%
Dim conn, connStr, dbPath
dbPath = Server.MapPath("../data/regdb.mdb")
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath & ";"
Set conn = Server.CreateObject("ADODB.Connection")
On Error Resume Next
conn.Open connStr
If Err.Number <> 0 Then
Response.Write "資料庫連接失敗: " & Err.Description
Response.End
End If
On Error GoTo 0
%>
<%@ LANGUAGE="VBSCRIPT" CODEPAGE="65001" %>
<% Response.Charset = "UTF-8" %>
<!-- #include file="conn.asp" -->
<html>
<head>
<meta charset="UTF-8">
<title>會員註冊</title>
<style>
.error { color: red; } .success { color: green; }
label { display: block; margin: 10px 0 5px; }
</style>
</head>
<body>
<h2>會員註冊</h2>
<form method="post" action="register_process.asp">
<label>帳號: <input type="text" name="username" required maxlength="50"></label>
<label>密碼: <input type="password" name="password" required minlength="6" maxlength="20"></label>
<label>確認密碼: <input type="password" name="password2" required></label>
<label>Email: <input type="email" name="email" required maxlength="100"></label>
<button type="submit">註冊</button>
</form>
</body>
</html>
<%@ LANGUAGE="VBSCRIPT" CODEPAGE="65001" %>
<% Response.Charset = "UTF-8" %>
<!-- #include file="conn.asp" -->
<%
Dim username, password, password2, email, sql, rs, hashedPwd
username = Trim(Request.Form("username"))
password = Request.Form("password")
password2 = Request.Form("password2")
email = Trim(Request.Form("email"))
' 伺服端驗證
If username = "" Or password = "" Or email = "" Then
Response.Write "所有欄位均為必填"
Response.End
End If
If password <> password2 Then
Response.Write "兩次密碼不一致"
Response.End
End If
If Len(password) < 6 Then
Response.Write "密碼至少 6 碼"
Response.End
End If
' 密碼雜湊 (此處示範簡單 Hash,實務請用 BCrypt/ASPClassicPBKDF2)
hashedPwd = GetPasswordHash(password) ' 自訂函數
' 檢查帳號是否存在
sql = "SELECT U_ID FROM Users WHERE U_Username = ?"
Set cmd = Server.CreateObject("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandText = sql
cmd.CommandType = 1
cmd.Parameters.Append cmd.CreateParameter("@u", 200, 1, 50, username)
Set rs = cmd.Execute
If Not rs.EOF Then
Response.Write "帳號已存在"
rs.Close: Set rs = Nothing
cmd.ActiveConnection = Nothing: Set cmd = Nothing
Response.End
End If
rs.Close: Set rs = Nothing
' 參數化新增
sql = "INSERT INTO Users (U_Username, U_Password, U_Email, U_RegDate, U_Status) VALUES (?, ?, ?, NOW(), 1)"
Set cmd = Server.CreateObject("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandText = sql
cmd.CommandType = 1
cmd.Parameters.Append cmd.CreateParameter("@user", 200, 1, 50, username)
cmd.Parameters.Append cmd.CreateParameter("@pwd", 200, 1, 255, hashedPwd)
cmd.Parameters.Append cmd.CreateParameter("@email", 200, 1, 100, email)
cmd.Execute
Response.Write "註冊成功!前往登入"
cmd.ActiveConnection = Nothing
Set cmd = Nothing
conn.Close
Set conn = Nothing
' 簡易雜湊示範 (生產環境勿用)
Function GetPasswordHash(pwd)
' 實務請引入 ASPClassicPBKDF2 或呼叫 COM 元件
GetPasswordHash = "HASH_" & pwd & "_SALT"
End Function
%>
此範例展示 ASP 經典註冊流程的現代化寫法:參數化、雜湊、驗證、錯誤處理,為後續會員系統奠定堅實基礎。