View sourcecode

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

login.php

55 lines ASCII Windows (CRLF)
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
<?php
require "database.php";
session_start();

if (isset(
$_SESSION['player_id'])) {
    
header("Location: dashboard.php");
    exit;
}

$error "";

if (
$_SERVER["REQUEST_METHOD"] === "POST") {
    
$stmt $conn->prepare("SELECT * FROM Players WHERE Username = ?");
    
$stmt->execute([$_POST["username"]]);
    
$user $stmt->fetch(PDO::FETCH_ASSOC);

    if (
$user && password_verify($_POST["password"], $user["PasswordHash"])) {
        
$_SESSION["player_id"] = $user["PlayerID"];
        
$_SESSION["username"] = $user["Username"];

        
header("Location: dashboard.php");
        exit;
    } else {
        
$error "Invalid login";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<div class="container mt-5">
    <h2>Login</h2>

    <?php if ($error): ?>
        <div class="alert alert-danger"><?= $error ?></div>
    <?php endif; ?>

    <form method="POST">
        <input class="form-control mb-2" name="username" placeholder="Username" required>
        <input class="form-control mb-2" type="password" name="password" placeholder="Password" required>

        <button class="btn btn-primary">Login</button>
        <a href="index.php" class="btn btn-secondary">Back</a>
    </form>
</div>

</body>
</html>