View sourcecode

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

bank.php

122 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
<?php  
include('check_login.php');

$username $_SESSION['username'];

$usersFile 'users.json';
$bankFile 'bank.json';

// Läs in användare och bankdata
$users file_exists($usersFile) ? json_decode(file_get_contents($usersFile), true) : [];
$bankData file_exists($bankFile) ? json_decode(file_get_contents($bankFile), true) : [];

$message '';
$fel '';

// Skapa konto med 1000 kr om det inte finns
if(!isset($bankData[$username])){
    
$bankData[$username] = [
        [
"typ" => "insättning""belopp" => 1000]
    ];
    
file_put_contents($bankFilejson_encode($bankDataJSON_PRETTY_PRINT));
}

// Hantera borttagning av konto
if (isset($_POST['deleteAccount'])) {
    unset(
$users[$username]);      // ta bort användare
    
unset($bankData[$username]);   // ta bort bankdata

    
file_put_contents($usersFilejson_encode($usersJSON_PRETTY_PRINT));
    
file_put_contents($bankFilejson_encode($bankDataJSON_PRETTY_PRINT));

    
session_destroy();
    
header("Location: index.php?message=Konto borttaget");
    exit;
}

// Hantera lösenordsbyte
if (isset($_POST['newPassword'])) {
    
$newPassword trim($_POST['newPassword']);
    if (!empty(
$newPassword)) {
        
$users[$username] = password_hash($newPasswordPASSWORD_DEFAULT);
        
file_put_contents($usersFilejson_encode($usersJSON_PRETTY_PRINT));
        
$message "Lösenord uppdaterat!";
    }
}

// Hantera insättning/uttag
if (isset($_POST['belopp'], $_POST['typ'])) {
    
$belopp = (int)$_POST['belopp'];
    
$typ $_POST['typ'];

    
$saldo 0;
    foreach (
$bankData[$username] as $t) {
        
$saldo += ($t['typ'] === 'insättning') ? $t['belopp'] : -$t['belopp'];
    }

    if (
$typ === 'uttag' && $belopp $saldo){
        
$fel "Du har inte tillräckligt med pengar!";
    } elseif (
$belopp <= 0){
        
$fel "Beloppet måste vara större än 0.";
    } else {
        
$bankData[$username][] = ["typ" => $typ"belopp" => $belopp];
        
file_put_contents($bankFilejson_encode($bankDataJSON_PRETTY_PRINT));
        
header("Location: bank.php");
        exit;
    }
}

// Beräkna saldo
$saldo 0;
foreach (
$bankData[$username] as $t) {
    
$saldo += ($t['typ'] === 'insättning') ? $t['belopp'] : -$t['belopp'];
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<title>Min Bank</title>
<link rel="stylesheet" href="style_p01.css">
</head>
<body>
<div class="container">
    <h1>Hej <?php echo htmlspecialchars($username); ?>!</h1>
    <p>Ditt saldo är: <strong><?php echo $saldo?> kr</strong></p>

    <?php if (!empty($fel)) echo "<p style='color:red;'>$fel</p>"?>
    <?php if (!empty($message)) echo "<p style='color:green;'>$message</p>"?>

    <form method="post">
        <label>Belopp:</label>
        <input type="number" name="belopp" required>
        <select name="typ">
            <option value="insättning">Insättning</option>
            <option value="uttag">Uttag</option>
        </select>
        <input type="submit" value="Utför">
    </form>

    <h3>Transaktioner:</h3>
    <ul>
    <?php foreach ($bankData[$username] as $t): ?>
        <li><?php echo ucfirst($t['typ']) . ": " $t['belopp'] . " kr"?></li>
    <?php endforeach; ?>
    </ul>

    <h3>Byt lösenord</h3>
    <form method="post">
        Nytt lösenord: <input type="password" name="newPassword" required><br>
        <input type="submit" value="Byt lösenord">
    </form>

    <h3>Ta bort konto</h3>
    <form method="post">
        <input type="submit" name="deleteAccount" value="Ta bort konto">
    </form>

    <p><a href="logout.php">Logga ut</a></p>
</div>
</body>
</html>