View sourcecode

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

login.php

63 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
<?php require "config.php"?>
<!DOCTYPE html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <title>Logga in</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">

<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-5">

            <div class="card shadow-sm">
                <div class="card-header bg-primary text-white text-center">
                    <h3>Logga in</h3>
                </div>

                <div class="card-body">
                    <?php
                    
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                        
$stmt $db->prepare("SELECT * FROM users WHERE username = ?");
                        
$stmt->execute([$_POST['username']]);
                        
$user $stmt->fetch();

                        if (
$user && password_verify($_POST['password'], $user['password'])) {
                            
$_SESSION['user_id'] = $user['id'];
                            
header("Location: dashboard.php");
                            exit;
                        } else {
                            echo 
'<div class="alert alert-danger">Fel användarnamn eller lösenord.</div>';
                        }
                    }
                    
?>

                    <form method="post">
                        <div class="mb-3">
                            <label class="form-label">Användarnamn</label>
                            <input name="username" class="form-control" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Lösenord</label>
                            <input name="password" type="password" class="form-control" required>
                        </div>

                        <button class="btn btn-primary w-100">Logga in</button>
                    </form>

                    <p class="text-center mt-3">
                        <a href="register.php">bli medlem</a>
                    </p>
                </div>
            </div>

        </div>
    </div>
</div>

</body>
</html>