diff --git a/change_password.php b/change_password.php new file mode 100644 index 0000000..d347edd --- /dev/null +++ b/change_password.php @@ -0,0 +1,114 @@ += 8 && strlen($pw) <= 64 + && preg_match('/[A-Za-z]/', $pw) && preg_match('/\d/', $pw) + && preg_match('/^[\x21-\x7e]+$/', $pw); // 可打印 ASCII,禁空格/中文 +} + +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['change_password']) && !$locked) { + if (!csrf_ok()) { + $message = '请求校验失败,请刷新页面重试。'; + } else { + $current = $_POST['current_password'] ?? ''; + $new = $_POST['new_password'] ?? ''; + + if (!strong_password_ok($new)) { + $message = $password_rule . '。'; + } elseif ($new === $current) { + $message = '新密码不能与当前密码相同。'; + } else { + $stmt = $conn->prepare("SELECT password FROM photo_users WHERE user_id = ?"); + $stmt->bind_param("i", $user['user_id']); + $stmt->execute(); + $hash = $stmt->get_result()->fetch_assoc()['password'] ?? ''; + $stmt->close(); + + if (!password_verify($current, $hash)) { + $message = '当前密码错误。'; + } else { + $newHash = password_hash($new, PASSWORD_DEFAULT); + $upd = $conn->prepare("UPDATE photo_users SET password = ?, must_change_password = 0 WHERE user_id = ?"); + $upd->bind_param("si", $newHash, $user['user_id']); + $ok = $upd->execute(); + $upd->close(); + $message = $ok ? '密码已修改成功。' : '修改失败,请稍后再试。'; + if ($ok && $forced) { + $_SESSION['login_pending_pwd'] = false; + flash_set('success', '密码已更新,欢迎使用'); + header($user['is_admin'] ? 'Location: admin.php' : 'Location: gallery.php'); + exit; + } + } + } + } +} + +render_header('修改密码', $user, 'change_password.php', '账号安全'); +?> +