一个简单的ASP数据库注册登录界面
时间: 2023-07-30 16:06:00 浏览: 43
以下是一个简单的 ASP 数据库注册登录界面的示例代码:
注册页面(register.asp):
```
<%@ Language=VBScript %>
<%
if Request.Form("submit") <> "" then
dim strConn, conn, cmd, sql, rs
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("db.mdb")
set conn = Server.CreateObject("ADODB.Connection")
set cmd = Server.CreateObject("ADODB.Command")
conn.Open strConn
cmd.ActiveConnection = conn
cmd.CommandType = adCmdText
cmd.CommandText = "SELECT * FROM users WHERE username='" & Request.Form("username") & "'"
set rs = cmd.Execute
if rs.EOF then
cmd.CommandText = "INSERT INTO users (username, password) VALUES ('" & Request.Form("username") & "', '" & Request.Form("password") & "')"
cmd.Execute
Response.Redirect "login.asp"
else
Response.Write "用户名已存在!"
end if
rs.Close
conn.Close
set rs = Nothing
set cmd = Nothing
set conn = Nothing
end if
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>注册</title>
</head>
<body>
<form name="form1" method="post" action="">
<p>
<label>用户名:
<input type="text" name="username" id="username">
</label>
</p>
<p>
<label>密码:
<input type="password" name="password" id="password">
</label>
</p>
<p>
<input type="submit" name="submit" id="submit" value="注册">
</p>
</form>
</body>
</html>
```
登录页面(login.asp):
```
<%@ Language=VBScript %>
<%
if Request.Form("submit") <> "" then
dim strConn, conn, cmd, sql, rs
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("db.mdb")
set conn = Server.CreateObject("ADODB.Connection")
set cmd = Server.CreateObject("ADODB.Command")
conn.Open strConn
cmd.ActiveConnection = conn
cmd.CommandType = adCmdText
cmd.CommandText = "SELECT * FROM users WHERE username='" & Request.Form("username") & "' AND password='" & Request.Form("password") & "'"
set rs = cmd.Execute
if rs.EOF then
Response.Write "用户名或密码错误!"
else
Session("username") = Request.Form("username")
Response.Redirect "welcome.asp"
end if
rs.Close
conn.Close
set rs = Nothing
set cmd = Nothing
set conn = Nothing
end if
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>登录</title>
</head>
<body>
<form name="form1" method="post" action="">
<p>
<label>用户名:
<input type="text" name="username" id="username">
</label>
</p>
<p>
<label>密码:
<input type="password" name="password" id="password">
</label>
</p>
<p>
<input type="submit" name="submit" id="submit" value="登录">
</p>
</form>
</body>
</html>
```
欢迎页面(welcome.asp):
```
<%@ Language=VBScript %>
<%
if Session("username") = "" then
Response.Redirect "login.asp"
end if
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>欢迎</title>
</head>
<body>
<p>欢迎您,<%=Session("username")%>!</p>
<a href="logout.asp">退出</a>
</body>
</html>
```
注意:以上示例代码仅供参考,实际应用中需要根据具体情况进行修改和完善。同时,为了保障安全性,应对用户输入的数据进行严格过滤和验证。