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

index.php

64 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>index</title>
    <?php
    
include('dbconnection.php');
    
/** @var PDO $dbconn */
    
$username $_SESSION['username'];

    
// Gå till loginsida om ingen är inloggad
    
if ($username === null) {
        
header("Location: login.php");
        exit;
    }

    if (isset(
$_SESSION['user_id'])) {
        
$user_id $_SESSION['user_id'];
        if (isset(
$_POST['submit'])) {
            
$room_code $_POST['roomcode'];

            echo (
$room_code);

            
$stmt $dbconn->prepare(
                
"SELECT id FROM fq_rooms WHERE room_code = :room_code"
            
);
            
$stmt->execute([':room_code' => $room_code]);
            
$room_id $stmt->fetchColumn();
            if (
$room_id) {
                
$dbconn->prepare('INSERT INTO fq_room_players (room_id, user_id) VALUES (?, ?)')->execute([$room_id$user_id]);
                
$_SESSION['room_id'] = $room_id;
                
$_SESSION['room_code'] = $room_code;
                
header("Location: waiting.php");
                exit;
            } else {
                
$error "Rummet hittades inte, försök igen!";
            }
        }
    }

    
?>
</head>

<body>
    <h1>Välkommen <?php echo ($username)  ?></h1>
    <form method="post" action="logout.php">
        <button type="submit">Logga ut</button>
    </form>
    <br>
    <form method="post" action="createroom.php">
        <button type="submit">Skapa Rum</button>
    </form>
    <p>Gå med i ett rum:</p>
    <form method="post">
        <input type="text" maxlength="7" name="roomcode">
        <input type="submit" name="submit">
    </form>
    <?php if (isset($error)) echo "<p class='error'>$error</p>"?>
</body>

</html>