如何用PHP实现用户登录界面
用户登录功能是现代Web应用中最基础也是最重要的功能之一,本文将详细介绍如何使用PHP从零开始实现一个完整的用户登录界面,包括前端表单设计、后端验证逻辑以及安全性考虑。
准备工作
在开始之前,确保你的开发环境已经安装了以下软件:
- PHP(建议版本7.4或更高)
- MySQL或MariaDB数据库
- Web服务器(如Apache或Nginx)
- 现代浏览器用于测试
数据库设计
我们需要创建一个存储用户信息的数据库表,以下是一个简单的用户表示例:
CREATE DATABASE IF NOT EXISTS user_auth; USE user_auth; CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL, email VARCHAR(100) NOT NULL UNIQUE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
注意:密码字段使用VARCHAR(255)是为了存储经过哈希处理后的密码,而不是明文密码。
创建登录表单(前端)
创建一个名为login.php
的文件,包含以下HTML代码:
<!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; display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #f4f4f4; } .login-container { background-color: white; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0,0,0,0.1); width: 300px; } .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; } button { width: 100%; padding: 10px; background-color: #5cb85c; color: white; border: none; border-radius: 4px; cursor: pointer; } button:hover { background-color: #4cae4c; } .error { color: red; margin-bottom: 10px; display: none; } </style> </head> <body> <div class="login-container"> <h2>用户登录</h2> <div class="error" id="error-message"> <?php if(isset($_GET['error'])) { echo htmlspecialchars($_GET['error']); } ?> </div> <form action="login_process.php" 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> <button type="submit">登录</button> </form> </div> </body> </html>
处理登录逻辑(后端)
创建一个名为login_process.php
的文件来处理登录请求:
<?php // 启动会话 session_start(); // 数据库连接配置 $db_host = 'localhost'; $db_user = 'root'; $db_pass = ''; $db_name = 'user_auth'; // 创建数据库连接 $conn = new mysqli($db_host, $db_user, $db_pass, $db_name); // 检查连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 检查是否是POST请求 if ($_SERVER["REQUEST_METHOD"] == "POST") { // 获取表单数据 $username = $_POST['username']; $password = $_POST['password']; // 准备SQL语句防止SQL注入 $stmt = $conn->prepare("SELECT id, username, password FROM users WHERE username = ?"); $stmt->bind_param("s", $username); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows === 1) { $user = $result->fetch_assoc(); // 验证密码(假设密码是使用password_hash哈希的) if (password_verify($password, $user['password'])) { // 密码正确,设置会话变量 $_SESSION['user_id'] = $user['id']; $_SESSION['username'] = $user['username']; // 重定向到用户主页 header("Location: dashboard.php"); exit(); } else { // 密码错误 $error = "用户名或密码错误"; header("Location: login.php?error=" . urlencode($error)); exit(); } } else { // 用户不存在 $error = "用户名或密码错误"; header("Location: login.php?error=" . urlencode($error)); exit(); } $stmt->close(); } $conn->close(); ?>
创建用户注册功能(可选)
为了测试登录功能,我们需要先创建一些用户,下面是一个简单的注册页面register.php
:
<!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; display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #f4f4f4; } .register-container { background-color: white; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0,0,0,0.1); width: 300px; } .form-group { margin-bottom: 15px; } label { display: block; margin-bottom: 5px; } input[type="text"], input[type="password"], input[type="email"] { width: 100%; padding: 8px; box-sizing: border-box; border: 1px solid #ddd; border-radius: 4px; } button { width: 100%; padding: 10px; background-color: #5cb85c; color: white; border: none; border-radius: 4px; cursor: pointer; } button:hover { background-color: #4cae4c; } .error { color: red; margin-bottom: 10px; } </style> </head> <body> <div class="register-container"> <h2>用户注册</h2> <?php if(isset($_GET['error'])) { echo '<div class="error">' . htmlspecialchars($_GET['error']) . '</div>'; } if(isset($_GET['success'])) { echo '<div style="color: green; margin-bottom: 10px;">注册成功,请<a href="login.php">登录</a></div>'; } ?> <form action="register_process.php" 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="email">邮箱:</label> <input type="email" id="email" name="email" required> </div> <div class="form-group"> <label for="password">密码:</label> <input type="password" id="password" name="password" required> </div> <div class="form-group"> <label for="confirm_password">确认密码:</label> <input type="password" id="confirm_password" name="confirm_password" required> </div> <button type="submit">注册</button> </form> </div> </body> </html>
对应的处理文件register_process.php
:
<?php // 数据库连接配置 $db_host = 'localhost'; $db_user = 'root'; $db_pass = ''; $db_name = 'user_auth'; // 创建数据库连接 $conn = new mysqli($db_host, $db_user, $db_pass, $db_name); // 检查连接 if ($conn->
还没有评论,来说两句吧...