View sourcecode

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

index.php

80 lines UTF-8 Unix (LF)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php

session_start
();
require_once(
"databaseconnection.php");

if(isset(
$_POST["pwd"])){
    if(
$_POST["choice"] == "login"){
        
$sql "SELECT `password`, `role` FROM users WHERE username = :username LIMIT 1";
        
$stm $pdo->prepare($sql);
        
$stm->execute([
            
':username' => $_POST['usn']
        ]);
        
$info $stm->fetch(PDO::FETCH_ASSOC);

        if(!
$info or !password_verify($_POST["pwd"], $info["password"])){
            
header("location:index.php?mess=fail");
            exit();
        }
        else{
            
$_SESSION["username"] = $_POST["usn"];
            
$_SESSION["role"] = $info["role"];
            
header("location:account.php");
            exit();
        }
    }

    if(
$_POST["choice"]== "signup"){
        
$sql "SELECT 1 FROM users WHERE username = :username LIMIT 1"#kollar om inmatat användarnamn är upptagen
        
$stm $pdo->prepare($sql);
        
$stm->execute([
            
':username' => $_POST['usn']
        ]);

        if (
$stm->fetch()) {
            
header("location:index.php?mess=occupied");
            exit();
        }

        
$encryptedPass password_hash($_POST["pwd"], PASSWORD_DEFAULT); 

        
$sql "INSERT INTO users (`username`, `password`, `role`) 
                VALUES (:username, :password, :role);"
;
        
$stm $pdo->prepare($sql);
        
$stm->execute([
            
':username'   => $_POST["usn"],
            
':password' => $encryptedPass,
            
':role' => "admin"
        
]);
        
header("location:index.php?mess=CREATED");
        exit();
    }
}

?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Inloggning databas</title>
    <link rel="stylesheet" href="/spectre/spectre-master/dist/spectre.css">
</head>
<body style="text-align: center;">
    <br>
    <h1>Logga in eller skapa konto</h1>
    <form method="post" action="?">
        <label for="login">Log in</label>
        <input type="radio" id="login" name="choice" value="login" checked="checked">
        <?php echo" | ";?>
        <label for="signup">Sign up</label>
        <input type="radio" id="sigunp" name="choice" value="signup">
        <br><br>
        <input type="text" id="usn" name="usn" placeholder="Username:" required><br><br>
        <input type="password" id="pwd" name="pwd" placeholder="Password:" required><br><br>
        <input type="submit" class="btn btn-primary">
    </form>
</body>
</html>