The following files exists in this folder. Click to view.
bank.php152 lines UTF-8 Windows (CRLF) 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
<?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($transactionFile, FILE_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_EOL, FILE_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>