Webbserverprogrammering 1

Källkod

Följande filer och mappar finns under mappen webbserverprogrammering.
Mappar visas till vänster och filer till höger. Klicka på en fil eller mapp för att öppna nedan eller visa dess innehåll.

webbserverprogrammering/exercises/repetition/

4 filer

repetition.php
repetition_login.php
repetition_login_secret.php
special_repetition.php

repetition.php

123 lines UTF-8 Windows (CRLF)
<?php
error_reporting
(-1); // Report all type of errors
ini_set('display_errors'1); // Display all errors 
ini_set('output_buffering'0); // Do not buffer outputs, write directly

session_start();
?>

<!DOCTYPE html>
<html lang="sv">
<head>
 <title>Repetition</title>
 <meta charset="utf-8">
</head>
<body>
 <h1>Repetition</h1>

 <?php

 $array 
= [];
 
$value 1;

 for (
$i 0$i 10$i++) { 
  
$array[] = $value;
  
$value *= 2;
 }

 foreach (
$array as $element) {
  echo 
"$element<br>";
 }
 echo 
"<br>Medelvärde: " . (array_sum($array) / count($array));

 echo 
"<hr><br>";

 function 
getBiggestNr($nr1$nr2$nr3) {
  return 
max($nr1$nr2$nr3);
 }

 
$biggest getBiggestNr(429);
 echo 
$biggest;

 echo 
"<br><hr><br>";

 function 
getAverage($array) {
  return 
array_sum($array) / count($array);
 }

 if (isset(
$_POST["value"]) && $_POST["value"] !== "") {
  
$array2 = [];
  for (
$i 0$i $_POST["value"]; $i++) { 
   
$array2[] = rand(110);
  }
  echo 
"<strong>Medelvärde för array2 (X antal slumpade, slumpat 1-10)</strong>:<br>" round(getAverage($array2), 3);
 }

 
?>
 <form method="post" action="">
  <input type="number" name="value" placeholder="hur många slumpningar"><br>
  <input type="submit" name="submitted" value="Beräkna">
 </form>

 <?php

 
echo "<br><hr><br>";

 
?>

 <form method="post" action="">
  <input type="text" name="inputName" placeholder="Namn"><br>
  <br>Maträtter
  <select name="food">
   <option value="pasta bolognese">Pasta bolognese</option>
   <option value="kebab">Kebab</option>
   <option value="janssons frestelse">Janssons frestelse</option>
  </select>
  <br>
  <input type="submit" name="submitted">
 </form>

 <?php

 
if (isset($_POST["inputName"])) {
  echo 
"<h1>Hej {$_POST['inputName']}! Du gillar {$_POST['food']}!</h1>";
 }

 echo 
"<br><hr><br>";

 
?>

 <form method="post" action="">
  
 <br>

 <?php

 
if (isset($_POST["guessNr"])) {
  if (isset(
$_POST["myName"])) {
   
$_SESSION["repetitionRandName"] = $_POST["myName"];
  }
  
$_SESSION["repetitionRandTries"]++;

  if (
$_POST["guessNr"] == $_SESSION["repetitionRandNr"]) {
   echo 
"Hej {$_SESSION['repetitionRandName']}! Rätt gissat! Det tog {$_SESSION['repetitionRandTries']} försök!<br>";
   
$_SESSION["repetitionRandTries"] = 0;
   
$_SESSION["repetitionRandNr"] = rand(115);
  }
  else {
   echo 
"Hej {$_SESSION['repetitionRandName']}! Fel gissat, du har nu gissat {$_SESSION['repetitionRandTries']} gånger!<br>";
  }
 }
 else {

  
$_SESSION["repetitionRandTries"] = 0;
  
$_SESSION["repetitionRandNr"] = rand(115);
  echo 
'<input type="text" name="myName" placeholder="Namn"><br>';
 }

 
?>
  <input type="number" name="guessNr" placeholder="Tal" min="1" max="15" width="100"><br>
  <input type="submit" name="submitted" value="Gissa tal (1-15)">
 </form>
</body>
</html>