Webbserverprogrammering 1

Show sourcecode

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

webbserver/filmquiz/

api/
createroom.php
createtables.php
createuser.php
css/
dbconnection.php
game.php
index.php
login.php
logout.php
remove.php
rensa.php
results.php
waiting.php

waiting.php

59 lines UTF-8 Windows (CRLF)
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="css/stylesheet.css" rel="stylesheet">
    <title>Waiting ...</title>
    <?php
    
include("dbconnection.php");
    
/** @var PDO $dbconn */
    
if (isset($_SESSION['username'])) {
        
$username $_SESSION['username'];
    } else {
        
header("Location: login.php");
        exit;
    }
    
$room_id $_SESSION['room_id'] ?? null;
    
$room_code $_SESSION['room_code'];
    
?>
</head>

<body>
    <h1>Väntar på spelare</h1>
    <p>
        Rum kod:
        <?php echo ($room_code); ?>
        <br>
    </p>
    <h3>Spelare:</h3>
    <p>
        <?php echo ($username); ?>
        <br><br>
        * Väntar på nästa spelare
    </p>

    <script>
        const room_id = <?php echo json_encode($room_id); ?>;

        // Ropa på api/roomplayers.php varje sekund tills vi har 2 spelare
        const interval = setInterval(async () => {
            try {
                const response = await fetch(`api/roomplayers.php?room_id=${room_id}`);
                const count = await response.json();
                console.log("room_id:", room_id, "count:", count);

                if (count >= 2) {
                    clearInterval(interval);
                    window.location.href = "game.php";

                }
            } catch (error) {
                console.error("Poll error:", error);
            }
        }, 1000);
    </script>
</body>

</html>