204 lines
6.8 KiB
PHP
204 lines
6.8 KiB
PHP
<?php
|
|||
|
|
// ============================================
|
||
|
|
// 文件: change_password.php
|
||
|
|
// 说明: 修改当前管理员的用户名和密码
|
||
|
|
// ============================================
|
||
|
|
|
||
|
|
require_once 'config.php';
|
||
|
|
checkLogin();
|
||
|
|
|
||
|
|
$admin = getCurrentAdmin();
|
||
|
|
$error = '';
|
||
|
|
$success = '';
|
||
|
|
|
||
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
|
|
$new_username = trim($_POST['new_username'] ?? '');
|
||
|
|
$old_password = $_POST['old_password'] ?? '';
|
||
|
|
$new_password = $_POST['new_password'] ?? '';
|
||
|
|
$confirm_password = $_POST['confirm_password'] ?? '';
|
||
|
|
|
||
|
|
$db = getDB();
|
||
|
|
|
||
|
|
// 验证旧密码
|
||
|
|
$stmt = $db->prepare("SELECT password FROM admins WHERE id = ?");
|
||
|
|
$stmt->execute([$admin['id']]);
|
||
|
|
$row = $stmt->fetch();
|
||
|
|
if (!password_verify($old_password, $row['password'])) {
|
||
|
|
$error = '旧密码错误';
|
||
|
|
} elseif (empty($new_username)) {
|
||
|
|
$error = '用户名不能为空';
|
||
|
|
} elseif (strlen($new_password) < 6) {
|
||
|
|
$error = '新密码长度至少6位';
|
||
|
|
} elseif ($new_password !== $confirm_password) {
|
||
|
|
$error = '两次输入的密码不一致';
|
||
|
|
} else {
|
||
|
|
// 检查用户名是否已被占用(排除自己)
|
||
|
|
$stmt = $db->prepare("SELECT COUNT(*) FROM admins WHERE username = ? AND id != ?");
|
||
|
|
$stmt->execute([$new_username, $admin['id']]);
|
||
|
|
if ($stmt->fetchColumn() > 0) {
|
||
|
|
$error = '用户名已被占用';
|
||
|
|
} else {
|
||
|
|
// 更新
|
||
|
|
$hashed = password_hash($new_password, PASSWORD_DEFAULT);
|
||
|
|
$stmt = $db->prepare("UPDATE admins SET username = ?, password = ? WHERE id = ?");
|
||
|
|
$stmt->execute([$new_username, $hashed, $admin['id']]);
|
||
|
|
|
||
|
|
// 更新session中的用户名
|
||
|
|
$_SESSION['admin_username'] = $new_username;
|
||
|
|
$success = '用户名和密码已更新,请重新登录';
|
||
|
|
// 可选:强制退出重新登录
|
||
|
|
// session_destroy();
|
||
|
|
// header('Location: login.php');
|
||
|
|
// exit;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
?>
|
||
|
|
<!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="css/style.css">
|
||
|
|
<style>
|
||
|
|
.form-container {
|
||
|
|
max-width: 500px;
|
||
|
|
margin: 0 auto;
|
||
|
|
padding: 20px;
|
||
|
|
}
|
||
|
|
.form-card {
|
||
|
|
background: var(--card-bg);
|
||
|
|
border-radius: 16px;
|
||
|
|
padding: 32px 36px;
|
||
|
|
box-shadow: var(--shadow);
|
||
|
|
}
|
||
|
|
.form-card h2 {
|
||
|
|
font-size: 24px;
|
||
|
|
font-weight: 700;
|
||
|
|
margin: 0 0 8px;
|
||
|
|
color: var(--text-primary);
|
||
|
|
}
|
||
|
|
.form-card .sub {
|
||
|
|
color: var(--text-secondary);
|
||
|
|
font-size: 14px;
|
||
|
|
margin-bottom: 24px;
|
||
|
|
}
|
||
|
|
.form-group {
|
||
|
|
margin-bottom: 20px;
|
||
|
|
}
|
||
|
|
.form-group label {
|
||
|
|
display: block;
|
||
|
|
font-weight: 600;
|
||
|
|
font-size: 14px;
|
||
|
|
color: var(--text-primary);
|
||
|
|
margin-bottom: 6px;
|
||
|
|
}
|
||
|
|
.form-group input {
|
||
|
|
width: 100%;
|
||
|
|
padding: 12px 16px;
|
||
|
|
border: 1px solid var(--border-color);
|
||
|
|
border-radius: 10px;
|
||
|
|
font-size: 15px;
|
||
|
|
background: var(--input-bg);
|
||
|
|
color: var(--text-primary);
|
||
|
|
box-sizing: border-box;
|
||
|
|
transition: border-color 0.2s;
|
||
|
|
}
|
||
|
|
.form-group input:focus {
|
||
|
|
outline: none;
|
||
|
|
border-color: var(--primary);
|
||
|
|
box-shadow: 0 0 0 4px rgba(230,126,34,0.12);
|
||
|
|
}
|
||
|
|
.form-actions {
|
||
|
|
display: flex;
|
||
|
|
gap: 12px;
|
||
|
|
margin-top: 20px;
|
||
|
|
}
|
||
|
|
.form-actions .btn {
|
||
|
|
padding: 12px 32px;
|
||
|
|
font-size: 16px;
|
||
|
|
}
|
||
|
|
.btn-secondary {
|
||
|
|
background: var(--bg-secondary);
|
||
|
|
color: var(--text-primary);
|
||
|
|
border: 1px solid var(--border-color);
|
||
|
|
}
|
||
|
|
.btn-secondary:hover {
|
||
|
|
background: var(--border-color);
|
||
|
|
}
|
||
|
|
.error-box {
|
||
|
|
background: rgba(231,76,60,0.08);
|
||
|
|
border: 1px solid rgba(231,76,60,0.2);
|
||
|
|
color: #e74c3c;
|
||
|
|
padding: 12px 16px;
|
||
|
|
border-radius: 10px;
|
||
|
|
margin-bottom: 20px;
|
||
|
|
font-size: 14px;
|
||
|
|
}
|
||
|
|
.success-box {
|
||
|
|
background: rgba(39,174,96,0.08);
|
||
|
|
border: 1px solid rgba(39,174,96,0.2);
|
||
|
|
color: #27ae60;
|
||
|
|
padding: 12px 16px;
|
||
|
|
border-radius: 10px;
|
||
|
|
margin-bottom: 20px;
|
||
|
|
font-size: 14px;
|
||
|
|
}
|
||
|
|
@media (max-width: 600px) {
|
||
|
|
.form-card {
|
||
|
|
padding: 20px 16px;
|
||
|
|
}
|
||
|
|
.form-actions {
|
||
|
|
flex-direction: column;
|
||
|
|
}
|
||
|
|
.form-actions .btn {
|
||
|
|
width: 100%;
|
||
|
|
text-align: center;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
</style>
|
||
|
|
</head>
|
||
|
|
<body>
|
||
|
|
<?php include 'nav.php'; ?>
|
||
|
|
|
||
|
|
<main class="main-content">
|
||
|
|
<div class="form-container">
|
||
|
|
<div class="form-card">
|
||
|
|
<h2>🔐 修改密码</h2>
|
||
|
|
<p class="sub">更新您的登录凭证</p>
|
||
|
|
|
||
|
|
<?php if ($error): ?>
|
||
|
|
<div class="error-box"><?= h($error) ?></div>
|
||
|
|
<?php endif; ?>
|
||
|
|
<?php if ($success): ?>
|
||
|
|
<div class="success-box"><?= h($success) ?></div>
|
||
|
|
<?php endif; ?>
|
||
|
|
|
||
|
|
<form method="POST" action="">
|
||
|
|
<div class="form-group">
|
||
|
|
<label>新用户名</label>
|
||
|
|
<input type="text" name="new_username" value="<?= h($admin['username'] ?? '') ?>" required>
|
||
|
|
</div>
|
||
|
|
<div class="form-group">
|
||
|
|
<label>旧密码</label>
|
||
|
|
<input type="password" name="old_password" placeholder="输入当前密码" required>
|
||
|
|
</div>
|
||
|
|
<div class="form-group">
|
||
|
|
<label>新密码 <span class="hint">至少6位</span></label>
|
||
|
|
<input type="password" name="new_password" placeholder="输入新密码" required>
|
||
|
|
</div>
|
||
|
|
<div class="form-group">
|
||
|
|
<label>确认新密码</label>
|
||
|
|
<input type="password" name="confirm_password" placeholder="再次输入新密码" required>
|
||
|
|
</div>
|
||
|
|
<div class="form-actions">
|
||
|
|
<button type="submit" class="btn btn-primary">💾 保存修改</button>
|
||
|
|
<a href="index.php" class="btn btn-secondary">取消</a>
|
||
|
|
</div>
|
||
|
|
</form>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
</main>
|
||
|
|
</body>
|
||
|
|
</html>
|