Källkodsbrowser

register.php

84 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
<?php
$msg 
'';
require 
'db_connect.php';

if (
$_SERVER['REQUEST_METHOD'] === 'POST') {
    
$username trim($_POST['username']);
    
$password trim($_POST['password']);

    
$exists false;

    
// Kolla om användarnamnet redan finns i databasen
    
$stmt $pdo->prepare("SELECT COUNT(*) FROM users WHERE username = ?");
    
$stmt->execute([$username]);
    if (
$stmt->fetchColumn() > 0) {
        
$exists true;
    }

    if (
$exists) {
        
$msg "Användarnamnet finns redan.";
    } elseif (
$username && $password) {
        
// Hasha lösenordet
        
$hashed_password password_hash($passwordPASSWORD_DEFAULT);

        try {
            
// Lägg till ny användare med roll "user" i databasen
            
$stmt $pdo->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, 'user')");
            
$stmt->execute([$username$hashed_password]);

            
// Ge nytt konto startsaldo 1000 kr
            
$stmt $pdo->prepare("INSERT INTO trasaktioner (username, amount, type) VALUES (?, ?, 'credit')");
            
$stmt->execute([$username1000]);

            
header("Location: index.php?msg=Konton är skapat!");
            exit();
        } catch (
\PDOException $e) {
            
error_log("Database error during registration: " $e->getMessage());
            
$msg "Kunde inte skapa konto. Försök igen.";
        }
    } 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: #1b91f8; border: none; color: white; font-size: 16px; border-radius: 5px; cursor: pointer; transition: background 0.3s; }
        button:hover { background: #0268dc; }
        .message { text-align: center; margin-bottom: 15px; }
        .back-link { display: block; text-align: center; margin-top: 20px; color: #1b91f8; text-decoration: none; transition: color 0.3s; }
        .back-link:hover { color: #0268dc; }
    </style>
</head>
<body>
    <div class="container">
        <h2>Registrera konto</h2>

        <?php if ($msg)
            echo 
"<p class='message'>$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>