Källkodsbrowser

register.php

144 lines UTF-8 Unix (LF)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?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($filePathFILE_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>