1. 准备工作:创建数据库和表。
  2. 创建登录页面:让用户输入用户名和密码。
  3. 创建验证页面:接收登录信息,连接数据库进行验证。
  4. 创建登录成功后的欢迎页面:显示登录成功的信息。
  5. 创建登出功能:让用户安全退出。

第1步:准备工作 - 创建数据库和表

你需要在 SQL Server 中创建一个数据库,并在其中创建一个用于存储用户信息的表。

网页 登录 asp sql数据库
(图片来源网络,侵删)

创建数据库 (可选) 如果你还没有数据库,可以使用以下 SQL 语句创建一个名为 MyWebDB 的数据库。

CREATE DATABASE MyWebDB;
GO

创建用户表 使用 USE 语句切换到你的数据库,然后创建一个 Users 表,这个表至少需要 UserID (主键), Username, 和 Password 字段。

USE MyWebDB;
GO
CREATE TABLE Users (
    UserID INT IDENTITY(1,1) PRIMARY KEY, -- 自动递增的主键
    Username NVARCHAR(50) NOT NULL UNIQUE, -- 用户名,不能为空且唯一
    Password NVARCHAR(100) NOT NULL,      -- 密码,实际应用中应该存储哈希值
    Email NVARCHAR(100)                   -- 邮箱(可选)
);
GO

插入一个测试用户 为了测试登录功能,我们向表中插入一条测试记录。

重要安全提示永远不要在数据库中存储明文密码! 这里的例子为了简化使用了明文,但在生产环境中,你必须对密码进行哈希处理(例如使用 BCryptSHA256)。

网页 登录 asp sql数据库
(图片来源网络,侵删)
INSERT INTO Users (Username, Password, Email)
VALUES ('admin', '123456', 'admin@example.com');
GO

你的数据库和表都已经准备好了。


第2步:创建登录页面 (login.asp)

这个页面是一个简单的 HTML 表单,包含用户名和密码输入框,以及一个提交按钮,当用户点击“登录”时,表单数据会被发送到验证页面。

login.asp 代码:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">用户登录</title>
    <style>
        body { font-family: Arial, sans-serif; background-color: #f4f4f4; }
        .login-container { width: 300px; margin: 100px auto; padding: 20px; background: #fff; border-radius: 5px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
        h2 { text-align: center; }
        .form-group { margin-bottom: 15px; }
        label { display: block; margin-bottom: 5px; }
        input[type="text"], input[type="password"] { width: 100%; padding: 8px; box-sizing: border-box; border: 1px solid #ddd; border-radius: 4px; }
        input[type="submit"] { width: 100%; padding: 10px; background-color: #5cb85c; color: white; border: none; border-radius: 4px; cursor: pointer; }
        input[type="submit"]:hover { background-color: #4cae4c; }
        .error-message { color: red; text-align: center; margin-bottom: 10px; }
    </style>
</head>
<body>
    <div class="login-container">
        <h2>用户登录</h2>
        <% 
        ' 检查URL中是否有错误参数,如果有则显示错误信息
        If Request.QueryString("error") = "1" Then
        %>
            <div class="error-message">用户名或密码错误!</div>
        <%
        End If
        %>
        <form action="check_login.asp" method="post">
            <div class="form-group">
                <label for="username">用户名:</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" name="password" required>
            </div>
            <input type="submit" value="登录">
        </form>
    </div>
</body>
</html>

第3步:创建验证页面 (check_login.asp)

这是核心部分,这个页面使用 VBScript 编写,它会:

  1. login.asp 接收用户名和密码。
  2. 使用 ADO (ActiveX Data Objects) 连接到 SQL Server 数据库。
  3. 执行 SQL 查询,检查用户名和密码是否匹配。
  4. 如果匹配,则创建一个 Session 变量来标记用户已登录,并重定向到欢迎页面。
  5. 如果不匹配,则重定向回登录页面,并附带一个错误参数。

check_login.asp 代码:

<%@ Language="VBScript" %>
<%
' 关闭错误提示,防止信息泄露
On Error Resume Next
' 1. 获取表单提交的数据
Dim username, password
username = Trim(Request.Form("username"))
password = Trim(Request.Form("password"))
' 简单的数据验证
If username = "" Or password = "" Then
    Response.Redirect("login.asp?error=1")
    Response.End
End If
' 2. 创建数据库连接对象
Dim conn, rs, connStr
Set conn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
' 3. 定义连接字符串 (请根据你的实际情况修改)
' Windows 身份验证 (推荐)
' connStr = "Provider=SQLOLEDB;Data Source=你的服务器名;Initial Catalog=MyWebDB;Integrated Security=SSPI;"
' SQL Server 身份验证
' 如果你的 SQL Server 使用了用户名和密码登录,请使用下面的连接字符串
' 请将 "your_server", "your_user", "your_password" 替换为你的实际信息
connStr = "Provider=SQLOLEDB;Data Source=你的服务器名或IP地址;Initial Catalog=MyWebDB;User ID=sa;Password=你的数据库密码;"
' 4. 打开数据库连接
conn.Open connStr
' 检查连接是否成功
If Err.Number <> 0 Then
    ' 连接失败,可以在这里记录错误日志
    Response.Write("数据库连接失败,请稍后再试。")
    Response.End
End If
' 5. 构建并执行 SQL 查询
' 使用参数化查询可以有效防止 SQL 注入攻击!
' 由于 VBScript 的参数化查询实现稍复杂,这里先用简单查询,但务必在实际应用中加强防范。
Dim sql
sql = "SELECT * FROM Users WHERE Username = '" & username & "' AND Password = '" & password & "'"
rs.Open sql, conn, 1, 3 ' 1=adOpenKeyset, 3=adLockOptimistic
' 6. 检查查询结果
If Not rs.EOF Then
    ' 登录成功!
    ' 创建 Session 变量,标记用户已登录
    Session("LoggedIn") = True
    Session("Username") = rs("Username") ' 可以存储更多用户信息
    ' 关闭记录集和连接
    rs.Close
    conn.Close
    Set rs = Nothing
    Set conn = Nothing
    ' 重定向到欢迎页面
    Response.Redirect("welcome.asp")
Else
    ' 登录失败
    rs.Close
    conn.Close
    Set rs = Nothing
    Set conn = Nothing
    ' 重定向回登录页面,并传递错误参数
    Response.Redirect("login.asp?error=1")
End If
%>

第4步:创建登录成功后的欢迎页面 (welcome.asp)

这个页面会检查 Session 变量 LoggedIn,如果该变量为 True,则显示欢迎信息;否则,说明用户未登录或会话已过期,将其重定向到登录页面。

welcome.asp 代码:

<%@ Language="VBScript" %>
<%
' 检查用户是否已登录
If Not Session("LoggedIn") Then
    ' 如果未登录,重定向到登录页面
    Response.Redirect("login.asp")
    Response.End
End If
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">欢迎页面</title>
    <style>
        body { font-family: Arial, sans-serif; background-color: #e9ecef; text-align: center; padding-top: 50px; }
        .welcome-container { background: white; padding: 30px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); display: inline-block; }
        h1 { color: #28a745; }
        p { font-size: 1.2em; }
        a { color: #007bff; text-decoration: none; }
        a:hover { text-decoration: underline; }
    </style>
</head>
<body>
    <div class="welcome-container">
        <h1>欢迎, <%= Session("Username") %>!</h1>
        <p>您已成功登录系统。</p>
        <p><a href="logout.asp">点击这里安全登出</a></p>
    </div>
</body>
</html>

第5步:创建登出功能 (logout.asp)

登出功能非常简单,只需要销毁当前的 Session 即可。

logout.asp 代码:

<%@ Language="VBScript" %>
<%
' 销毁所有 Session 变量
Session.Abandon()
' 重定向回登录页面
Response.Redirect("login.asp")
%>

总结与重要提示

  1. 安全性

    • 密码哈希check_login.asp 中的密码是明文存储和验证的,这是非常危险的,你应该在注册和登录时对密码进行哈希处理(例如使用 BCrypt 组件)。
    • SQL 注入check_login.asp 中的 SQL 查询直接拼接了字符串,这极易受到 SQL 注入攻击。强烈推荐使用参数化查询
    • 关闭错误提示:在 check_login.asp 中使用 On Error Resume Next 可以向用户暴露数据库结构等敏感信息,在生产环境中,应该将错误记录到日志文件,而不是直接显示给用户。
  2. 部署

    • 你需要一个支持 ASP 的 Web 服务器,最常见的是 IIS (Internet Information Services),它通常安装在 Windows Server 操作系统上。
    • 确保你的 IIS 已经安装了 "ASP" 功能。
    • 将所有 .asp 文件放在 IIS 的网站根目录下(C:\inetpub\wwwroot)。
  3. 连接字符串

    • check_login.asp 中的连接字符串是你最需要根据自己环境修改的地方。Data Source 通常是你的计算机名(如果是本地数据库)或 IP 地址。User IDPassword 是你的 SQL Server 登录凭据,如果使用 Windows 身份验证,则 Integrated Security=SSPI 更方便安全。

将这四个文件 (login.asp, check_login.asp, welcome.asp, logout.asp) 放到你的 IIS 网站目录下,通过浏览器访问 http://localhost/你的虚拟目录名/login.asp 即可开始测试。