Webbserverprogrammering 1

Show sourcecode

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

webbsrvprg/exercises/quiz/

adminlogin.php
createtables.php
createtest.php
dbconnection.php
index.php
kundsida.php
result.php
test.php

index.php

99 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
include('dbconnection.php');
date_default_timezone_set("Europe/Stockholm")
?>
<?php
session_start
();
$warningMsg "";
if (isset(
$_POST["login"])) {
    try {
    
$sql "SELECT id, username, password FROM users";
    
$stmt $dbconn->prepare($sql);
    
$data = array();
    
$stmt->execute($data);
    while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
        if (
$_POST["user"] === $res['username'] && $_POST["password"] === $res['password']) {
            
$_POST["user"] === $_SESSION['username'];
            
$_SESSION["logIn"] = true;
            
$_SESSION["AlogIn"] = false;
            
$_SESSION["userid"] = $res["id"];
            
$sql "UPDATE users SET login_date=? 
            WHERE username=?"
;
            
$stmt $dbconn->prepare($sql);
            
$data = array(date("Y-m-d H:i:s"), $_POST["user"]);
            
$stmt->execute($data);
            
header("Location: kundsida.php");
        } else
            echo 
"Fel uppgifter!<br>";
    }
    
}
catch(
PDOException $e)
{
    echo 
$sql "<br />" $e->getMessage();
}
}
if (isset(
$_POST["create"])) {
    try {
    
$sql "INSERT INTO users (name, username, password, login_date) 
    VALUES (?, ?, ?, now())"
;
    
$stmt $dbconn->prepare($sql);
    
$data = array($_POST["name"], $_POST["user"], $_POST["password"]);
    
$stmt->execute($data);
    
$_SESSION["logIn"] = true;
    
header("Location: kundsida.php");
}
catch(
PDOException $e)
{
    echo 
$sql "<br />" $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
<nav>
        <a href="index.php">Login</a> <br>
        <a href="result.php">Resultat</a> <br>
        <a href="kundsida.php">Kundsida</a> <br>
        <a href="adminlogin.php">Admin</a>
    </nav>
    <div style="display: flex; justify-content:space-around; margin: auto">
        <div>
            <h1>Logga in</h1>
        <form method="post" action="">
            <br>
            <input type="text" name="user" placeholder="Användarnamn" required><br>
            <br>
            <input type="password" name="password" placeholder="Lösenord" required><br>
            <br>
            <input type="submit" name="login">
        </form>
    </div>
    <div>
        <h1>Skapa konto</h1>
        <form method="post" action="">
            <br>
            <input type="text" name="name" placeholder="Namn" required><br>
            <br>
            <input type="text" name="user" placeholder="Användarnamn" required><br>
            <br>
            <input type="password" name="password" placeholder="Lösenord" required><br>
            <br>
            <input type="submit" name="create">
        </form>
    </div>
</div>
</body>

</html>