<?php
$msg = '';
$filePath = __DIR__ . "/users.txt";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
// Skapa filen om den inte finns
if (!file_exists($filePath))
file_put_contents($filePath, "");
// Läs in alla användare
$lines = file($filePath, FILE_IGNORE_NEW_LINES);
$exists = false;
foreach ($lines as $line) {
list($u, , ) = explode(";", $line);
if ($u === $username)
$exists = true; // Kolla om användarnamnet redan finns
}
if ($exists)
$msg = "Användarnamnet finns redan.";
elseif ($username && $password) {
// Lägg till ny användare med roll "user"
file_put_contents($filePath, "$username;$password;user\n", FILE_APPEND);
header("Location: index.php?msg=Konto skapat! Du kan nu logga in.");
exit();
} else
$msg = "Du måste fylla i både användarnamn och lösenord.";
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<title>Registrera konto</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
body {
background: #f0f2f5;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background: #fff;
padding: 40px;
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
}
h2 {
text-align: center;
margin-bottom: 30px;
color: #333;
}
label {
display: block;
margin-bottom: 5px;
color: #555;
}
input[type="text"],
input[type="password"] {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border-radius: 5px;
border: 1px solid #ccc;
}
button {
width: 100%;
padding: 12px;
background: #1b91f8ff;
border: none;
color: white;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s;
}
button:hover {
background: #0268dcff;
}
.message {
text-align: center;
margin-bottom: 15px;
}
.back-link {
display: block;
text-align: center;
margin-top: 20px;
color: #1b91f8ff;
text-decoration: none;
transition: color 0.3s;
}
.back-link:hover {
color: #0268dcff;
}
</style>
</head>
<body>
<div class="container">
<h2>Registrera konto</h2>
<?php if ($msg)
echo "<p style='color:green;'>$msg</p>"; ?>
<form method="post">
<label>Användarnamn:</label>
<input type="text" name="username" placeholder="Användarnamn" required>
<label>Lösenord:</label>
<input type="password" name="password" placeholder="Lösenord" required>
<button type="submit">Registrera</button>
</form>
<a class="back-link" href="index.php">Tillbaka till inloggning</a>
</div>
</body>
</html>