Webbserverprogrammering 1

Show sourcecode

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

webbsrvprg/exercises/cookie-session/

ovn_cook1.php
ovn_cook2.php
ovn_cook3.php
ovn_cook3hem.php
ovn_cook4.php
ovn_cook5.php

ovn_cook4.php

53 lines UTF-8 Windows (CRLF)
<?php
session_start
(); 

if (!isset(
$_SESSION['tal'])) {
  
$_SESSION['tal'] = array();
}

$total 0;
?>

<!DOCTYPE html>
<html lang="sv">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Cookie-Session 4</title>
</head>
<body>

  <form action="" method="post">
    Skriv in tal:
    <input type="text" name="tal" required> <br>
    <input type="submit">
  </form>

  <?php
  
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset(
$_POST["tal"]) && ($_POST["tal"] != "")) {
      
array_push($_SESSION['tal'], $_POST["tal"]);
    }
  }

  if (!empty(
$_SESSION['tal'])) {
    foreach (
$_SESSION['tal'] as $tal) {
      
$total += $tal;
    }

    
$antalTal count($_SESSION['tal']);
    
$medelvarde $total $antalTal;

    echo 
"<h3>Inmatade tal:</h3>";
    echo 
"<pre>";
    
print_r($_SESSION['tal']);
    echo 
"</pre>";

    echo 
"<h3>Medelvärde:</h3> $medelvarde";
  }
  
?>
  
</body>
</html>