View sourcecode

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

login.php

73 lines ASCII 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
<?php
session_start
();
require_once 
"functions/database.php";

function 
attempt_login($username$password)
{
    global 
$db;
    
$result $db->query("SELECT userID, `password` FROM accountData WHERE username = :username", array("username" => $username));
    if (!
$result)
        return 
null;
    
$hash hash("sha256"$passwordfalse);
    if (
$result["password"] == $hash)
        return 
$result["userID"];
    return 
null;
}

$login_failed false;
if (isset(
$_POST["username"]) && isset($_POST["password"])) {
    
$userID attempt_login($_POST["username"], $_POST["password"]);
    if (
$userID)
        
$_SESSION["userID"] = $userID;
    else
        
$login_failed true;
}

if(isset(
$_SESSION["userID"])){
    
header("location: index.php");
    exit();
}

?>

<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Log in</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI"
        crossorigin="anonymous"></script>
</head>

<body>
    <?php include "page/navbar.php" ?>
    <div class="container">
        <br>
        <h1>Log in</h1><br>
        <?php if ($login_failed)
            echo 
'<p class="text-danger">Login failed!</p>'?>
        <form action="login.php" method="POST">
            <label for="username" class="form-label">Username</label>
            <div class="input-group mb-3">
                <input type="text" id="username" name="username" class="form-control" placeholder="Username"
                    aria-label="Username" aria-describedby="basic-addon1" required>
            </div>
            <br>
            <label for="password" class="form-label">Password</label>
            <div class="input-group mb-3">
                <input type="password" id="password" name="password" class="form-control" placeholder="Password"
                    aria-label="Password" aria-describedby="basic-addon1" required>
            </div>
            <input type="submit" class="btn btn-primary" value="Log in">
            <br>
            <a href="signup.php">Sign up instead</a>
        </form>
    </div>
</body>

</html>