Webbserverprogrammering 1

Show sourcecode

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

webbsrvprg/exercises/

hej.php
hej.py
index.php
mysqlintro_1.php
ovn_cookie1.php
ovn_cookie2.php
ovn_cookie3_1.php
ovn_cookie3_2.php
ovn_cookie4.php
ovn_cookie5.php
ovn_cookie6.php
ovn_form1.php
ovn_form2.php
ovn_form3_1.php
ovn_form3_2.php
ovn_form7_1.php
ovn_form7_2.php
ovn_form8_1.php
ovn_form_tillamp1_1.php
ovn_form_tillamp1_2.php
ovn_form_tillamp2.php
ovn_form_tillamp3.php
ovn_form_tillamp4.php
ovn_form_tillamp5.php
ovn_form_tillamp6.php
ovn_funk1.php
ovn_funk2.php
ovn_funk3.php
ovn_funk4.php
ovn_funk5.php
ovn_funk6.php
ovn_funk7.php
ovn_funk8.php
ovn_funk9.php
ovn_gr1.php
ovn_gr2.php
ovn_gr3.php
ovn_gr4.php
ovn_gr5.php
ovn_gr6.php
ovn_gr7.php
ovn_incl1/
ovn_incl2/
ovn_incl3/
ovn_klass1.php
ovn_klass2.php
ovn_klass3.php
ovn_klass4.php
ovn_klass5.php
ovn_klass6.php
ovn_klass7.php
ovn_mysqlintro1_1.php
ovn_mysqlintro1_2.php
ovn_str1_1.php
ovn_str1_2.php
ovn_str2_1.php
ovn_str2_2.php
ovn_str3_1.php
ovn_str3_2.php
ovn_str4_1.php
ovn_str4_2.php
ovn_str5_1.php
ovn_str5_2.php
ovn_str6_1.php
ovn_str6_2.php

ovn_form_tillamp3.php

97 lines UTF-8 Windows (CRLF)
<?php
session_start
(); // Starta sessionen högst upp

if (!isset($_SESSION['steg'])) {
    
$_SESSION['steg'] = 1
    
$_SESSION['korrektaSvar'] = 0
    
$_SESSION['name'] = ''
}

if (
$_SERVER['REQUEST_METHOD'] == 'POST') {
    if (
$_SESSION['steg'] == && isset($_POST['name'])) {
        
$_SESSION['name'] = $_POST['name'];
        
$_SESSION['steg'] = 2// Nästa steg
    
} elseif ($_SESSION['steg'] == && isset($_POST['fraga1'])) {
        if (
$_POST['fraga1'] == "2") {
            
$_SESSION['korrektaSvar']++;
        }
        
$_SESSION['steg'] = 3// Nästa steg
    
} elseif ($_SESSION['steg'] == && isset($_POST['fraga2'])) {
        if (
$_POST['fraga2'] == "3") {
            
$_SESSION['korrektaSvar']++;
        }
        
$_SESSION['steg'] = 4// Nästa steg
    
} elseif ($_SESSION['steg'] == && isset($_POST['fraga3'])) {
        if (
$_POST['fraga3'] == "4") {
            
$_SESSION['korrektaSvar']++;
        }
        
$_SESSION['steg'] = 5// Sista steg
    
}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Frågetävling</title>
</head>
<body>

<?php
// Visa olika formulär beroende på vilket steg vi är på
if ($_SESSION['steg'] == 1) {
    
// Formulär för att mata in namn
    
echo '<form action="" method="POST">
            <h2>Vad är ditt namn?</h2>
            <input type="text" name="name" required>
            <button type="submit">Nästa</button>
          </form>'
;
} elseif (
$_SESSION['steg'] == 2) {
    
// Fråga 1
    
echo '<form action="" method="POST">
            <h2>Vad är 1+1?</h2>
            <select name="fraga1">
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
              <option value="4">4</option>
            </select>
            <button type="submit">Nästa</button>
          </form>'
;
} elseif (
$_SESSION['steg'] == 3) {
    
// Fråga 2
    
echo '<form action="" method="POST">
            <h2>Vad är 1+2?</h2>
            <select name="fraga2">
              <option value="2">2</option>
              <option value="3">3</option>
              <option value="4">4</option>
              <option value="5">5</option>
            </select>
            <button type="submit">Nästa</button>
          </form>'
;
} elseif (
$_SESSION['steg'] == 4) {
    
// Fråga 3
    
echo '<form action="" method="POST">
            <h2>Vad är 1+3?</h2>
            <select name="fraga3">
              <option value="3">3</option>
              <option value="4">4</option>
              <option value="5">5</option>
              <option value="6">6</option>
            </select>
            <button type="submit">Slutför</button>
          </form>'
;
} elseif (
$_SESSION['steg'] == 5) {
    echo 
'<h2>Hej, ' htmlspecialchars($_SESSION['name']) . '!</h2>';
    echo 
'<p>Du fick ' $_SESSION['korrektaSvar'] . ' av 3 rätt.</p>';

    
// Återställ sessionen för att börja om
    
session_destroy();
}
?>

</body>
</html>