The following files exists in this folder. Click to view.
register.php61 lines UTF-8 Windows (CRLF) 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
<?php
session_start();
$usersFile = "users.txt";
$error = "";
$success = "";
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$username = trim($_POST["username"] ?? "");
$password = trim($_POST["password"] ?? "");
if ($username === "" || $password === "") {
$error = "Alla fält måste fyllas i";
} else {
if (!file_exists($usersFile)) {
file_put_contents($usersFile, "");
}
$users = file($usersFile, FILE_IGNORE_NEW_LINES);
foreach ($users as $user) {
list($existingUser) = explode("|", $user);
if ($existingUser === $username) {
$error = "Användarnamnet finns redan";
break;
}
}
if ($error === "") {
file_put_contents($usersFile, "$username|$password" . PHP_EOL, FILE_APPEND);
$success = "Konto skapat. Du kan nu logga in.";
}
}
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>XTG-banken</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<h2>Skapa konto</h2>
<?php if ($error): ?>
<div style="color:red"><?= $error ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div style="color:green"><?= $success ?></div>
<?php endif; ?>
<form method="post">
<input name="username" placeholder="Användarnamn">
<input name="password" type="password" placeholder="Lösenord">
<button type="submit">Skapa konto</button>
</form>
<a href="login.php">Tillbaka till inloggning</a>