<?php
require 'check_login.php';
$user = $_SESSION['username'];
$filePath = __DIR__ . "/users.txt";
$msg = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$old = $_POST['old'];
$new = $_POST['new'];
$lines = file($filePath, FILE_IGNORE_NEW_LINES);
foreach ($lines as &$line) {
list($u, $p, $r) = explode(";", $line);
if ($u === $user && $p === $old) {
$line = "$u;$new;$r";
file_put_contents($filePath, implode("\n", $lines) . "\n");
$msg = "Lösenordet har ändrats!";
break;
}
}
if (!$msg)
$msg = "Fel nuvarande lösenord.";
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<title>Byt lösenord</title>
<style>
body {
font-family: Arial;
background: #f0f2f5;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.box {
background: white;
padding: 40px;
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
width: 350px;
text-align: center;
}
input,
button {
width: 100%;
padding: 10px;
margin: 10px 0;
border-radius: 5px;
border: 1px solid #ccc;
}
button {
background: #1b91f8;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background: #0268dc;
}
a {
display: inline-block;
margin-top: 10px;
color: #1b91f8;
text-decoration: none;
}
</style>
</head>
<body>
<div class="box">
<h2>Byt lösenord</h2>
<?php if ($msg): ?>
<p><?= htmlspecialchars($msg) ?></p><?php endif; ?>
<form method="post">
<input type="password" name="old" placeholder="Nuvarande lösenord" required>
<input type="password" name="new" placeholder="Nytt lösenord" required>
<button type="submit">Byt lösenord</button>
</form>
<a href="user.php">Tillbaka</a>
</div>
</body>
</html>