106 lines
3.9 KiB
PHP
106 lines
3.9 KiB
PHP
<?php
|
|
require_once 'auth.php';
|
|
require_once '../includes/db.php';
|
|
|
|
$success = '';
|
|
$error = '';
|
|
|
|
// 处理密码修改
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$old_password = $_POST['old_password'] ?? '';
|
|
$new_password = $_POST['new_password'] ?? '';
|
|
$confirm_password = $_POST['confirm_password'] ?? '';
|
|
|
|
// 验证输入
|
|
if (empty($old_password) || empty($new_password) || empty($confirm_password)) {
|
|
$error = '所有字段都必须填写';
|
|
} elseif ($new_password !== $confirm_password) {
|
|
$error = '两次输入的新密码不一致';
|
|
} elseif (strlen($new_password) < 4) {
|
|
$error = '新密码长度至少4位';
|
|
} else {
|
|
// 验证旧密码
|
|
$stmt = $db->prepare("SELECT * FROM users WHERE id = ?");
|
|
$stmt->execute([$_SESSION['user_id']]);
|
|
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($user && password_verify($old_password, $user['password'])) {
|
|
// 更新密码
|
|
$new_password_hash = password_hash($new_password, PASSWORD_DEFAULT);
|
|
$stmt = $db->prepare("UPDATE users SET password = ? WHERE id = ?");
|
|
|
|
if ($stmt->execute([$new_password_hash, $_SESSION['user_id']])) {
|
|
$success = '密码修改成功!';
|
|
} else {
|
|
$error = '密码修改失败,请重试';
|
|
}
|
|
} else {
|
|
$error = '旧密码错误';
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>修改密码</title>
|
|
<link rel="stylesheet" href="../assets/admin.css">
|
|
</head>
|
|
<body>
|
|
<div class="admin-container">
|
|
<header class="admin-header">
|
|
<h1>修改密码</h1>
|
|
<div class="header-actions">
|
|
<a href="dashboard.php" class="btn btn-secondary">返回管理面板</a>
|
|
<a href="logout.php" class="btn btn-secondary">退出</a>
|
|
</div>
|
|
</header>
|
|
|
|
<main class="admin-main">
|
|
<?php if ($success): ?>
|
|
<div class="alert alert-success"><?php echo htmlspecialchars($success); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-error"><?php echo htmlspecialchars($error); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<section class="config-section">
|
|
<h2>修改登录密码</h2>
|
|
<form method="POST" class="config-form">
|
|
<div class="form-group">
|
|
<label>当前密码 *</label>
|
|
<input type="password" name="old_password" required autofocus>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>新密码 *</label>
|
|
<input type="password" name="new_password" required minlength="4">
|
|
<small class="form-hint">密码长度至少4位</small>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>确认新密码 *</label>
|
|
<input type="password" name="confirm_password" required minlength="4">
|
|
</div>
|
|
|
|
<button type="submit" class="btn btn-primary">修改密码</button>
|
|
</form>
|
|
</section>
|
|
|
|
<section class="config-section">
|
|
<h2>安全提示</h2>
|
|
<ul style="color: #666; line-height: 1.8; padding-left: 20px;">
|
|
<li>密码长度建议至少8位,包含字母、数字和特殊字符</li>
|
|
<li>不要使用过于简单的密码,如:123456、admin 等</li>
|
|
<li>定期更换密码以提高安全性</li>
|
|
<li>不要将密码告诉他人</li>
|
|
</ul>
|
|
</section>
|
|
</main>
|
|
</div>
|
|
</body>
|
|
</html>
|