View sourcecode

The following files exists in this folder. Click to view.

register.php

61 lines UTF-8 Windows (CRLF)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?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($usersFileFILE_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|$passwordPHP_EOLFILE_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>