View sourcecode

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

bank.php

152 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
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
145
146
147
148
149
150
151
152
<?php


session_start
();

if (!isset(
$_SESSION["user"])) {
    
header("Location: login.php");
    exit;
}

$user $_SESSION["user"];



$transactionFile "transactions_{$user}.txt";
if (!
file_exists($transactionFile)) {
    
$initialAmount 1000;
    
$time date("Y-m-d H:i:s");
    
file_put_contents($transactionFile$initialAmount "|" $time PHP_EOL);
}

$transactions file($transactionFileFILE_IGNORE_NEW_LINES FILE_SKIP_EMPTY_LINES);

$balance 0;
foreach (
$transactions as $transactionLine) {
    list(
$amount$date) = explode("|"$transactionLine);
    
$balance += (int)$amount;
}

$message '';
if (
$_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset(
$_POST['deleteAccount'])) {
        
unlink($transactionFile);
        
header("Location: " $_SERVER['PHP_SELF']);
        exit;
    }

    
$amount = isset($_POST['belopp']) ? intval($_POST['belopp']) : 0;
    
$type $_POST['type'] ?? 'insättning';

    if (
$amount 0) {
        if (
$type === 'uttag' && $amount $balance) {
            
$message "Uttag kan inte vara större än saldot";
        } else {
            
$amount = ($type === 'uttag') ? -$amount $amount;
            
$time date("Y-m-d H:i:s");
            
file_put_contents($transactionFile$amount "|" $time PHP_EOLFILE_APPEND);
            
header("Location: " $_SERVER['PHP_SELF']);
            exit;
        }
    } else {
        
$message "Ange ett giltigt belopp";
    }
}
?>

<!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>
<body class="bg-light">
    <div class="bg-dark text-white py-3 mb-4">
        <div class="container text-center">
            <h1>XTG-banken</h1>
            <p>Välkommen till XTG-banken</p>
        </div>
    </div>

    <div class="text-end mb-3">
    <span>Inloggad som <strong><?= htmlspecialchars($user?></strong></span>
    <a href="logout.php" class="btn btn-outline-danger btn-sm ms-2">Logga ut</a>
</div>

    <main class="container">
        <div class="row">
            <div class="col-md-6 mb-4">
                <div class="card">
                    <div class="card-header">
                        <h5>Saldo</h5>
                        <p class="fs-4"><?= $balance ?> kr</p>
                    </div>
                    <div class="card-body">
                        <h5>Insättning / Uttag</h5>
                        <?php if (!empty($message)) : ?>
                            <div class="alert alert-danger"><?= $message ?></div>
                        <?php endif; ?>
                        <form method="post">
                            <div class="mb-3">
                                <label for="belopp" class="form-label">Belopp</label>
                                <input type="text" class="form-control" id="belopp" name="belopp" placeholder="belopp">
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="type" id="insattning" value="insättning" checked>
                                <label class="form-check-label" for="insattning">Insättning</label>
                            </div>
                            <div class="form-check mb-3">
                                <input class="form-check-input" type="radio" name="type" id="uttag" value="uttag">
                                <label class="form-check-label" for="uttag">Uttag</label>
                            </div>
                            <button type="submit" class="btn btn-primary">Utför</button>
                        </form>
                    </div>
                </div>
            </div>
            <div class="col-md-6 mb-4">
                <div class="card">
                    <div class="card-header">
                        <h5>Transaktioner</h5>
                    </div>
                    <div class="card-body">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>Belopp</th>
                                    <th>Datum</th>
                                    <th>Saldo</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $runningBalance 
0;
                                foreach (
$transactions as $index => $transactionLine) {
                                    list(
$amount$date) = explode("|"$transactionLine);
                                    
$runningBalance += (int)$amount;
                                    
$class = ((int)$amount 0) ? 'text-danger' '';
                                    echo 
"<tr>";
                                    echo 
"<td>" . ($index 1) . "</td>";
                                    echo 
"<td class='$class'>" $amount " kr</td>";
                                    echo 
"<td>" $date "</td>";
                                    echo 
"<td>" $runningBalance " kr</td>";
                                    echo 
"</tr>";
                                }
                                
?>
                            </tbody>
                        </table>
                        <form method="post" onsubmit="return confirm('Är du säker på att du vill ta bort kontot?');">
                            <input type="hidden" name="deleteAccount" value="1">
                            <button type="submit" class="btn btn-danger mt-2">Ta bort konto</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </main>
</body>
</html>