Show sourcecode
The following files exists in this folder. Click to view.
cookie1.php
cookie2.php
cookie3login.php
cookie3secret.php
cookie4.php
cookie5.php
cookie4.php
48 lines UTF-8 Windows (CRLF)
<?php
$nums = [];
if (isset($_COOKIE['nums']) && is_array($_COOKIE['nums'])) {
$nums = $_COOKIE['nums'];
}
if (isset($_POST['num'])) {
$nums[] = $_POST['num'];
}
foreach ($nums as $i => $n) {
setcookie("nums[$i]", $n, time() + 60, "/");
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kakorna är tillbaka, allt är bra igen!</title>
</head>
<body>
<form method="post">
<label for="num">Skriv in ett nummer:</label>
<input type="number" name="num" id="num">
<input type="submit" value="Mata in nummer">
</form>
<?php
function medelvärde(&$array)
{
$sum = 0;
for ($i = 0; $i < count($array); $i++) {
$sum += $array[$i];
}
return $sum / count($array);
}
if (isset($nums)) {
echo "<p>Alla inlästa tal är: " . implode(", ", $nums) . "</p>";
echo "<p>Medelvärdet är: " . medelvärde($nums) . "</p>";
}
?>
</body>
</html>